Why this did not work?

kyleflow

Pirate
Registered
LV
0
 
Joined
Jan 12, 2026
Messages
19
Reaction score
3
Points
8
This is the same code from carribien map with the function name and etc is change. I dont know my iteration of this did not work. The map is clickable and if im not in position. it provide me the coordinate. and when I arrive at the expected location from coordinate, if it does not directly succeed in digging, it will be able to dig till succeed. BUT, after the succeed notification appeared, the map did not consume, the character stuck on place, no reward gain. Yes I update the iteminfo, Yes I made also a specific table for the reward in the variable.lua. So yeah I'm confused on why it did not work. I also ask GPT to compare the 2 code and it said its perfectly replicated.
Code:
function ItemUse_MAPK1(role, Item)
     local Item_CanGet = GetChaFreeBagGridNum(role)
    if Item_CanGet < 1 then
        SystemNotice(role ,"You need to have at least 1 empty inventory slot")
        UseItemFailed(role)
        return
    end
    local lv= GetChaAttr(role, ATTR_LV)
    if lv < 15 or  lv > 100 then
        SystemNotice(role, "Characters lower than Lv 15 or higher than Lv 40 cannot use this treasure map")
        UseItemFailed(role)
        return   
    end
    local Has_GoldenMap = CheckBagItem(role, 9630)
    if Has_GoldenMap >= 2 then
        SystemNotice(role, "You can only have 1 treasure map in inventory to seek for treasure. Please deposit the rest into your vault")
        UseItemFailed(role)
        return
    end
    local Item = GetChaItem2(role, 2, 9630)
    local Item_MAXURE = GetItemAttr(Item, ITEMATTR_MAXURE)
    local Item_URE = GetItemAttr(Item, ITEMATTR_URE)
    local Item_MAXENERGY = GetItemAttr(Item, ITEMATTR_MAXENERGY)
    local pos_x = Item_MAXURE
    local pos_y = Item_MAXENERGY
    local Themap = Item_URE
    local MapList = {}   
    MapList[0] = "NoMap"
    MapList[1] = "jialebi"
    local MapNameList = {}
    MapNameList[0] = "No map"
    MapNameList[1] = "Treasure Gulf"
    if pos_x == 0 or pos_y == 0 or Themap == 0 then
        pos_x, pos_y, Themap = GetTheMapPos_JLB(role, 1)   
        Item_MAXURE = pos_x
        Item_URE = Themap
        Item_MAXENERGY = pos_y
        SetItemAttr(Item, ITEMATTR_MAXENERGY, Item_MAXENERGY)
        SetItemAttr(Item, ITEMATTR_MAXURE, Item_MAXURE)
        SetItemAttr(Item, ITEMATTR_URE, Item_URE)
    end
    local GetPos = CheckGetMapPos(role, pos_x, pos_y, MapList[Themap])
    if GetPos == 0 then
        SystemNotice(role, "Treasure is hidden at "..MapNameList[Themap].." Region. "..pos_x..","..pos_y.." nearby")
        UseItemFailed(role)
        return
    elseif GetPos == 1 then
        local getrandom = math.random(1,3)
        if getrandom == 1 then
            Give_MAPK1(role)
        else
            SystemNotice(role, "Looks like nothing is dug out. Look again nearby")
            UseItemFailed(role)
            return
        end
    end
end
function Give_MAPK1(role)
    local CheckRandom = math.random(1,100)
    local x, y = GetChaPos(role)
    local monsterID = math.random(829,836)
    if CheckRandom >= 1 and CheckRandom <= 20 then
        local GiveMoney = 10000 * math.random (1, 20)
        SystemNotice (role, "Dug out Caribbean Treasure and obtain "..GiveMoney.."G")
        AddMoney (role, 0, GiveMoney)
    elseif CheckRandom > 20 and CheckRandom <= 24 then
        XianJing(role, 1)
    elseif CheckRandom > 24 and CheckRandom <= 28 then
        XianJing(role, 2)
    elseif CheckRandom > 28 and CheckRandom <= 33 then
        SystemNotice(role, "Today seems to be spining about. Don't know where it will spin to")
        MapRandomtele(role)
    elseif CheckRandom > 33 and CheckRandom <= 40 then
        SystemNotice(role , "Oh no! Who let the dogs out! Help...")
        local new1 = CreateCha(monsterID, x, y, 145, 30)
        SetChaLifeTime(new1, 90000)
    else
        SystemNotice (role, "dug out a hidden pirate treasure")
        local General = 0       
        for i = 1 , #BaoXiang_MAPK1, 1 do
            if BaoXiang_MAPK1[i].Active == 1 then
                General = BaoXiang_MAPK1[i].Rad + General
            end
        end
        local a = math.random(1, General)
        local b = 0
        local d = 0
        local c = -1
        for k = 1 , #BaoXiang_MAPK1, 1 do
            if BaoXiang_MAPK1[k].Active == 1 then
                d = BaoXiang_MAPK1[k].Rad + b
                if a <= d and a > b then
                    c = k
                    break
                end
                b = d
            end
        end
        if c ~= -1 then
            local Item2Give = BaoXiang_MAPK1[c].ItemID
            local ItemCount = BaoXiang_MAPK1[c].Quantity
            local ItemQuality = BaoXiang_MAPK1[c].Quality
            local GoodItem = BaoXiang_MAPK1[c].GoodItem
            GiveItem(role, 0, Item2Give, ItemCount, ItemQuality)
            if GoodItem == 1 then
                local message = ""..GetChaDefaultName(role).." Dug out a treasure and obtained "..GetItemName(Item2Give)..""
                Notice(message)
            end
        end
    end
end
 
Your code isn't the problem. I diffed it against the original ItemUse_JLB_GoldenMap / GiveGoldenMapItem_JLB and it's the same logic. What you're seeing is a Lua runtime error inside Give_MAPK1. When a use-script errors partway through, the server never finishes the item use. The map isn't consumed, the client never gets its reply (that's why your character freezes), and the error is written to the GameServer's Lua error log. Check that log first. It will tell you the exact line.

The message you see is almost certainly "dug out a hidden pirate treasure". The very next thing that runs is #BaoXiang_MAPK1, so it's nearly always one of these:

The table doesn't exist at runtime. variable.lua is loaded once when the GameServer starts, from skilleffect.lua. If you reloaded only ItemEffect.lua or restarted only the client, your new function exists but BaoXiang_MAPK1 is still nil. Fully restart the GameServer.
You edited the wrong copy. Make sure you edited the SERVER's resource/script/calculate/variable.lua, not the client's copy.
The name doesn't match. Lua names are case-sensitive. BaoXiang_MAPK1 must match exactly.
The weights add up to 0. If no row has Active = 1 with a Rad above 0, then math.random(1, 0) throws "interval is empty". Every row needs Active, ItemID, Quantity, Quality, Rad and GoodItem, like the BaoXiang_JLBCBTBOX rows.
An ItemID in your table doesn't exist in ItemInfo.

To catch case 1 in-game, put this at the top of ItemUse_MAPK1:

if BaoXiang_MAPK1 == nil or #BaoXiang_MAPK1 == 0 then
SystemNotice(role, "BaoXiang_MAPK1 is not loaded - restart the GameServer")
UseItemFailed(role)
return
end

Also, the original spawns the dogs with CreateChaX(monsterID, x, y, 145, 30, role). You changed it to CreateCha(monsterID, x, y, 145, 30). That's fine if your server has CreateCha, but it's another place that can error in the 7% "dogs" branch.
 
Thanks for extensive reply @Panda , i will check 1 by 1 by your recommendation and response back if anything is coming back right. About the CreateCha, I use the source files, i assume it has it since the original code is from there. But still I will double check it again.
 
Thanks for extensive reply @Panda , i will check 1 by 1 by your recommendation and response back if anything is coming back right. About the CreateCha, I use the source files, i assume it has it since the original code is from there. But still I will double check it again.
Let me know if it doesn't work we can keep going through it