• 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 bad argument #1 to 'pairs' (table expected, got number)

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
I'm using the same script on windows (test server) and on my server (linux), but when I kill the dungeon monsters on the linux server, it keeps giving this error, does anyone know how to solve it?

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:eek:nDeath
data/global.lua:118: bad argument #1 to 'pairs' (table expected, got number)
stack traceback:
[C]: at 0x5555557fdf34
[C]: in function 'pairs'
data/global.lua:118: in function 'contains'
..rescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:31: in function <...rescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:27>

Lua:
config = {
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
}

function getRandomSpawnPosition(fromPosition, toPosition)
    local random = math.random
    return Position(random(fromPosition.x, toPosition.x), random(fromPosition.y, toPosition.y), random(fromPosition.z, toPosition.z))
end


local function dungeonMonster(name, position, fromPosition, toPosition)
    local tmpMonster = Game.createMonster(name, getRandomSpawnPosition(fromPosition, toPosition), true, false)
    
    if tmpMonster then       
        tmpMonster:registerEvent("dungeon_monster")
    end

end

function onDeath(monster, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)   
    local monsterName = monster:getName():lower()
    
    for configs, area in pairs(config) do
        if table.contains(configs, monsterName) then -- here           
            local randomTime = math.random(1, 3) 
            local monsterPosition = monster:getPosition()
            addEvent(dungeonMonster, randomTime * 60 * 100, monsterName, monsterPosition, area.fromPositionArea, area.toPositionArea)
        end
    end

    return true
end
 
Solution
Please when defining variables that will only be used in the file, make it local

Lua:
config = {
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
}
change to:
Lua:
local config = { -- here <<<< local :D
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition...
I'm using the same script on windows (test server) and on my server (linux), but when I kill the dungeon monsters on the linux server, it keeps giving this error, does anyone know how to solve it?

Lua Script Error: [CreatureScript Interface]
data/creaturescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:eek:nDeath
data/global.lua:118: bad argument #1 to 'pairs' (table expected, got number)
stack traceback:
[C]: at 0x5555557fdf34
[C]: in function 'pairs'
data/global.lua:118: in function 'contains'
..rescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:31: in function <...rescripts/scripts/SystemCustom/dungeon/dungeon_death.lua:27>

Lua:
config = {
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
}

function getRandomSpawnPosition(fromPosition, toPosition)
    local random = math.random
    return Position(random(fromPosition.x, toPosition.x), random(fromPosition.y, toPosition.y), random(fromPosition.z, toPosition.z))
end


local function dungeonMonster(name, position, fromPosition, toPosition)
    local tmpMonster = Game.createMonster(name, getRandomSpawnPosition(fromPosition, toPosition), true, false)
   
    if tmpMonster then      
        tmpMonster:registerEvent("dungeon_monster")
    end

end

function onDeath(monster, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)  
    local monsterName = monster:getName():lower()
   
    for configs, area in pairs(config) do
        if table.contains(configs, monsterName) then -- here          
            local randomTime = math.random(1, 3)
            local monsterPosition = monster:getPosition()
            addEvent(dungeonMonster, randomTime * 60 * 100, monsterName, monsterPosition, area.fromPositionArea, area.toPositionArea)
        end
    end

    return true
end
I don't use/know TFS functions, but i can't see where is your "area".
Lua:
for configs, area in pairs(config) do
 
Please when defining variables that will only be used in the file, make it local

Lua:
config = {
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
}
change to:
Lua:
local config = { -- here <<<< local :D
    [{'mutated rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
    [{'goblin', 'rat'}] = {
        fromPositionArea = Position(1001, 780, 7), -- fromPosition
        toPositionArea = Position(1097, 871, 7), -- toPosition
    },
}

Because apparently there is another global variable with the same name config that is overwriting your table in the file, so when trying to iterate over the config table, it does not seem to work because according to the key they are numbers, when really your table has keys of type table
 
Solution
Back
Top