• 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!

Issue with droploot.lua and tables.lua

Slaz99

New Member
Joined
Jul 30, 2014
Messages
3
Reaction score
0
Every time a player dies, the console shows this error:

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/others/droploot.lua:eek:nDeath
data/lib/core/tables.lua:17: bad argument #1 to 'pairs' (table expected, got number)
stack traceback:
[C]: at 0x7ff72bf30980
[C]: in function 'pairs'
data/lib/core/tables.lua:17: in function 'contains'
data/creaturescripts/scripts/others/droploot.lua:7: in function <data/creaturescripts/scripts/others/droploot.lua:1>

This is TFS 1.2

tables.lua
Lua:
table.append = table.insert
table.empty = function (t)
    return next(t) == nil
end

table.find = function (table, value)
    for i, v in pairs(table) do
        if(v == value) then
            return i
        end
    end

    return nil
end

table.contains = function (txt, str)
    for i, v in pairs(str) do
        if(txt:find(v) and not txt:find('(%w+)' .. v) and not txt:find(v .. '(%w+)')) then
            return true
        end
    end

    return false
end
table.isStrIn = table.contains

table.count = function (table, item)
    local count = 0
    for i, n in pairs(table) do
        if(item == n) then
            count = count + 1
        end
    end

    return count
end
table.countElements = table.count

table.getCombinations = function (table, num)
    local a, number, select, newlist = {}, #table, num, {}
    for i = 1, select do
        a[#a + 1] = i
    end

    local newthing = {}
    while(true) do
        local newrow = {}
        for i = 1, select do
            newrow[#newrow + 1] = table[a[i]]
        end

        newlist[#newlist + 1] = newrow
        i = select
        while(a[i] == (number - select + i)) do
            i = i - 1
        end

        if(i < 1) then
            break
        end

        a[i] = a[i] + 1
        for j = i, select do
            a[j] = a[i] + j - i
        end
    end

    return newlist
end

function table.serialize(x, recur)
    local t = type(x)
    recur = recur or {}

    if(t == nil) then
        return "nil"
    elseif(t == "string") then
        return string.format("%q", x)
    elseif(t == "number") then
        return tostring(x)
    elseif(t == "boolean") then
        return t and "true" or "false"
    elseif(getmetatable(x)) then
        error("Can not serialize a table that has a metatable associated with it.")
    elseif(t == "table") then
        if(table.find(recur, x)) then
            error("Can not serialize recursive tables.")
        end
        table.append(recur, x)

        local s = "{"
        for k, v in pairs(x) do
            s = s .. "[" .. table.serialize(k, recur) .. "]"
            s = s .. " = " .. table.serialize(v, recur) .. ","
        end
        s = s .. "}"
        return s
    else
        error("Can not serialize value of type '" .. t .. "'.")
    end
end

function table.unserialize(str)
    return loadstring("return " .. str)()
end

droploot.lua
Lua:
function onDeath(player, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    if getPlayerFlagValue(player, PlayerFlag_NotGenerateLoot) or player:getVocation():getId() == VOCATION_NONE then
        return true
    end

    local amulet = player:getSlotItem(CONST_SLOT_NECKLACE)
    if amulet and amulet.itemid == ITEM_AMULETOFLOSS and not table.contains({SKULL_RED, SKULL_BLACK}, player:getSkull()) then
        local isPlayer = false
        if killer then
            if killer:isPlayer() then
                isPlayer = true
            else
                local master = killer:getMaster()
                if master and master:isPlayer() then
                    isPlayer = true
                end
            end
        end

        if not isPlayer or not player:hasBlessing(6) then
            player:removeItem(ITEM_AMULETOFLOSS, 1, -1, false)
        end
    else
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local item = player:getSlotItem(i)
            if item then
                if table.contains({SKULL_RED, SKULL_BLACK}, player:getSkull()) or math.random(item:isContainer() and 100 or 1000) <= player:getLossPercent() then
                    if not item:moveTo(corpse) then
                        item:remove()
                    end
                end
            end
        end
    end

    if not player:getSlotItem(CONST_SLOT_BACKPACK) then
        player:addItem(ITEM_BAG, 1, false, CONST_SLOT_BACKPACK)
    end
    return true
end

SOLVED: deleted the file tables.lua and added the table.contains function in global.lua
 
Last edited:
Solution
Now I get this lua error

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/others/droploot.lua:eek:nDeath
data/creaturescripts/scripts/others/droploot.lua:27: attempt to call field 'contains' (a nil value)
stack traceback:
[C]: in function 'contains'
data/creaturescripts/scripts/others/droploot.lua:27: in function <data/creaturescripts/scripts/others/droploot.lua:1>

looks like you missing this function... add it to your global.lua

Code:
table.contains = function(array, value)
    for _, targetColumn in pairs(array) do
        if targetColumn == value then
            return true
        end
    end
    return false
end
delete your tables.lua, that's old code
table.contains is a function like isInArray, your tables.lua is overwriting it which causes issues in the original tfs code.
or at the very least remove the table.contains from your tables.lua, you have a function called table.find which does the same thing
 
Thanks, I'll make some tests and reply which is the best option

delete your tables.lua, that's old code
table.contains is a function like isInArray, your tables.lua is overwriting it which causes issues in the original tfs code.
or at the very least remove the table.contains from your tables.lua, you have a function called table.find which does the same thing

Now I get this lua error

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/others/droploot.lua:eek:nDeath
data/creaturescripts/scripts/others/droploot.lua:27: attempt to call field 'contains' (a nil value)
stack traceback:
[C]: in function 'contains'
data/creaturescripts/scripts/others/droploot.lua:27: in function <data/creaturescripts/scripts/others/droploot.lua:1>
 
Last edited by a moderator:
Now I get this lua error

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/others/droploot.lua:eek:nDeath
data/creaturescripts/scripts/others/droploot.lua:27: attempt to call field 'contains' (a nil value)
stack traceback:
[C]: in function 'contains'
data/creaturescripts/scripts/others/droploot.lua:27: in function <data/creaturescripts/scripts/others/droploot.lua:1>

looks like you missing this function... add it to your global.lua

Code:
table.contains = function(array, value)
    for _, targetColumn in pairs(array) do
        if targetColumn == value then
            return true
        end
    end
    return false
end
 
Solution
Back
Top