• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua [TFS 0.X] Error when kill 2 bosses with reward system

potinho

Advanced OT User
Joined
Oct 11, 2009
Messages
1,403
Solutions
17
Reaction score
151
Location
Brazil
Hello guys,

I have a script for Reward Chest (who send parcel to DP with loot, and calculate reward by damage or heal), but when people kill 2 bosses script got this error on console:

2023-06-01 20:46:51 - [Error - CreatureScript Interface]
2023-06-01 20:46:51 - In a timer event called from:
2023-06-01 20:46:51 - data/creaturescripts/scripts/Reward System/rewardchest_boss.lua:eek:nDeath
2023-06-01 20:46:51 - Description:
2023-06-01 20:46:51 - (luaGetCreatureStorage) Creature not found
2023-06-01 20:46:51 -
2023-06-01 20:46:51 - [Error - CreatureScript Interface]
2023-06-01 20:46:51 - In a timer event called from:
2023-06-01 20:46:51 - data/creaturescripts/scripts/Reward System/rewardchest_boss.lua:eek:nDeath
2023-06-01 20:46:51 - Description:
2023-06-01 20:46:51 - ...rescripts/scripts/Reward System/rewardchest_boss.lua:58: bad argument #1 to 'ceil' (number expected, got boolean)
2023-06-01 20:46:51 - stack traceback:
2023-06-01 20:46:51 - [C]: in function 'ceil'
2023-06-01 20:46:51 - ...rescripts/scripts/Reward System/rewardchest_boss.lua:58: in function <...rescripts/scripts/Reward System/rewardchest_boss.lua:46>

And follow file pointed:
Lua:
dofile('data/sistemas/rewardchest.lua')

local function addRewardLoot(uid, bossName, tabela_reward)
    local money = math.random(10, 40)
    local msg = "The following items are available in your reward chest:"
    local chest = doCreateItemEx(REWARDCHEST.rewardBagId)

    doItemSetAttribute(chest, "description", "Reward System has kill the boss ".. bossName ..".")

    if table.maxn(tabela_reward) > 0 then
        for x = 1, table.maxn(tabela_reward) do
            local rand = math.random(100)
            if rand <= tabela_reward[x][3] then
                local count = math.random(1, tabela_reward[x][2])
                doAddContainerItem(chest, tabela_reward[x][1], count)
                msg = msg .. " ".. (count > 1 and count or "") .." "..getItemNameById(tabela_reward[x][1])..","
            end
        end
        doPlayerSendTextMessage(uid, MESSAGE_INFO_DESCR, msg .. " and ".. money .." platinum coins.")
    else
        doPlayerSendTextMessage(uid, MESSAGE_INFO_DESCR, msg .. " ".. money .." platinum coins.")
    end

    doAddContainerItem(chest, 2152, money)
    doPlayerSendMailByName(getPlayerName(uid), chest, REWARDCHEST.town_id)

    local boss = REWARDCHEST.bosses[bossName]
    setPlayerStorageValue(uid, boss.storage, 0)
    doSendMagicEffect(getPlayerPosition(uid), CONST_ME_MAGIC_BLUE)
end

local function addLoot(tabela_loot, tabela_reward, all_loot)
    if table.maxn(tabela_loot) > 0 then
        if all_loot then
            for x = 1, table.maxn(tabela_loot) do
                table.insert(tabela_reward, tabela_loot[x])
            end
        else
            table.insert(tabela_reward, tabela_loot[math.random(table.maxn(tabela_loot))])
        end
    end

    return tabela_reward
end

local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)

    local porcentagem = math.ceil(getPlayerStorageValue(players[1], boss.storage))

    for i = 1, table.maxn(players) do

        local tabela_reward = {}
        local pontos = getPlayerStorageValue(players[i], boss.storage)

        if i == 1 then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
            addLoot(boss.sempre, tabela_reward, true)
        elseif i >= 2 and pontos >= math.ceil((porcentagem * 0.8)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
            addLoot(boss.muito_raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.8)) and pontos >= math.ceil((porcentagem * 0.6)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
            addLoot(boss.raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.6)) and pontos >= math.ceil((porcentagem * 0.4)) then
            addLoot(boss.comum, tabela_reward, false)
            addLoot(boss.semi_raro, tabela_reward, false)
        elseif pontos < math.ceil((porcentagem * 0.4)) and pontos >= math.ceil((porcentagem * 0.1)) then
            addLoot(boss.comum, tabela_reward, false)
        end

        addRewardLoot(players[i], bossName, tabela_reward)
    end
end

function onDeath(cid, corpse, killer)
    local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
    if boss then
        addEvent(rewardChestSystem, 1000, getCreatureName(cid):lower())
    end
    return true
end

function onStatsChange(cid, attacker, type, combat, value)
    if isMonster(cid) and type == STATSCHANGE_HEALTHLOSS and isPlayer(attacker) then
        local boss = REWARDCHEST.bosses[getCreatureName(cid):lower()]
        if boss and attacker then
            setPlayerStorageValue(attacker, boss.storage, getPlayerStorageValue(attacker, boss.storage) + math.ceil((value / REWARDCHEST.formula.hit)))
        end
    end
    return true
end

Someone knows what cause bug? Can help me to fix?
 
change
Lua:
local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)
to
Lua:
local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    if not players[1] then
        print("No players online with boss.storage > 0")
        return
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)
 
change
Lua:
local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)
to
Lua:
local function rewardChestSystem(bossName)
    local players = {}
    local boss = REWARDCHEST.bosses[bossName]

    for _, uid in ipairs(getPlayersOnline()) do
        if getPlayerStorageValue(uid, boss.storage) > 0 then
            table.insert(players, uid)
        end
    end

    if not players[1] then
        print("No players online with boss.storage > 0")
        return
    end

    table.sort(players, function(a, b) return getPlayerStorageValue(a, boss.storage) > getPlayerStorageValue(b, boss.storage) end)
With this print option:

Lua:
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:41 -  No players online with boss.storage > 0
2023-06-05 15:35:41 -  No players online with boss.storage > 0

Just remove "print" option to supress message and its fixed?
 
With this print option:

Lua:
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:40 -  No players online with boss.storage > 0
2023-06-05 15:35:41 -  No players online with boss.storage > 0
2023-06-05 15:35:41 -  No players online with boss.storage > 0

Just remove "print" option to supress message and its fixed?
Realistically yes..

But it shouldn't be triggering that often.

Since all your bosses share the same storage value to check damage.. there's a fundamental issue with the entire system, imo.

Since 2+ different bosses could be fighting players at the same time..

Boss 1 dies, and rewards all players that were fighting any boss.
Boss 2 dies, and the players get another reward. (whom were attacking the 2nd boss)

The fix from above will fix it triggering multiple times for a single boss.
But the ultimate fix would be to give each boss their own storage.
 
Realistically yes..

But it shouldn't be triggering that often.

Since all your bosses share the same storage value to check damage.. there's a fundamental issue with the entire system, imo.

Since 2+ different bosses could be fighting players at the same time..

Boss 1 dies, and rewards all players that were fighting any boss.
Boss 2 dies, and the players get another reward. (whom were attacking the 2nd boss)

The fix from above will fix it triggering multiple times for a single boss.
But the ultimate fix would be to give each boss their own storage.
There's a problem, if i have players killing different bosses at same time and boss die at same hour (0,00001% chance), just players for one boss will receive reward? Im attaching entire system if u want (and can) to take a look.
 

Attachments

Back
Top