• 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 1.3] check if a table is nil?

E

Evil Puncker

Guest
Sup everyone, I'm using the amazing raid script by @Nekiro , it works like a charm and all, but it also throws an error and I tried to "fix" that but I don't know where to begin with, so here I am asking for help, follows the script:

Lua:
local raids = {
    -- Bosses
    [1]  = {type = "BOSS", position = Position(49, 172, 10), monster = "Demodras", message = "Demodras has spawned", chance = 3},
    [2]  = {type = "BOSS", position = Position(115, 88, 7), monster = "Grorlam", message = "Grorlam has spawned", chance = 2},
    [3]  = {type = "BOSS", position = Position(102, 41, 8), monster = "Rotworm Queen", message = "Rotworm Queen has spawned", chance = 2},

    -- Raids fromPos, toPos
    [4] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = {"minotaur", "minotaur guard", "minotaur mage", "minotaur archer"}, count = 50, message = "Minotaur assault incoming!", chance = 20},
    [6] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = "Feverish Citizen", count = 50, message = "Feverish Citizens have invaded the streets!", chance = 10},
}

local spawnedBosses = {}

local function isSpawned(name)
    for i = 1, #spawnedBosses do
        local monster = Monster(spawnedBosses[i])
        if monster and monster:getName():lower() == name then
            return true
        else
            spawnedBosses[i] = nil
        end
    end
    return false
end

local globalevent = GlobalEvent("SpawnRaid")

function globalevent.onThink(...)
    local table = raids[math.random(#raids)]

    if math.random(100) > table.chance then
        return true
    end

    if table.type == "BOSS" then
        if not isSpawned(table.monster:lower()) then
            local boss = Game.createMonster(table.monster, table.position, true, false)
            if boss then
                spawnedBosses[#spawnedBosses + 1] = boss:getId()
            end
            if table.message ~= nil then
                broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
            end
        end
    elseif table.type == "NORMAL" then
        for i = 1, table.count do
            local randomPosition = Position(math.random(table.fromPos.x, table.toPos.x), math.random(table.fromPos.y, table.toPos.y), table.fromPos.z)
            if type(table.monsters) == "string" then
                Game.createMonster(table.monsters, randomPosition, true, false)
            else
                Game.createMonster(table.monsters[math.random(#table.monsters)], randomPosition, true, false)
            end
        end
        if table.message ~= nil then
            broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
        end
    end
    return true
end

globalevent:interval(1800000) -- will be executed every 1000ms
globalevent:register()

and the error:
\data\scripts\globalevents\raids.lua:31: attempt to index local 'table' (a nil value)
 
Solution
You have raids 1, 2, 3, 4, 6.
You're missing raid #5, either change 6 to 5 or add a new one

The problem is that you have 5 elements in the table, so math.random generates a random number from 1 to 5, so if the random number happens to be 5, it will look for raids[5] which does not exist.
You have raids 1, 2, 3, 4, 6.
You're missing raid #5, either change 6 to 5 or add a new one

The problem is that you have 5 elements in the table, so math.random generates a random number from 1 to 5, so if the random number happens to be 5, it will look for raids[5] which does not exist.
 
Solution
Sup everyone, I'm using the amazing raid script by @Nekiro , it works like a charm and all, but it also throws an error and I tried to "fix" that but I don't know where to begin with, so here I am asking for help, follows the script:

Lua:
local raids = {
    -- Bosses
    [1]  = {type = "BOSS", position = Position(49, 172, 10), monster = "Demodras", message = "Demodras has spawned", chance = 3},
    [2]  = {type = "BOSS", position = Position(115, 88, 7), monster = "Grorlam", message = "Grorlam has spawned", chance = 2},
    [3]  = {type = "BOSS", position = Position(102, 41, 8), monster = "Rotworm Queen", message = "Rotworm Queen has spawned", chance = 2},

    -- Raids fromPos, toPos
    [4] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = {"minotaur", "minotaur guard", "minotaur mage", "minotaur archer"}, count = 50, message = "Minotaur assault incoming!", chance = 20},
    [6] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = "Feverish Citizen", count = 50, message = "Feverish Citizens have invaded the streets!", chance = 10},
}

local spawnedBosses = {}

local function isSpawned(name)
    for i = 1, #spawnedBosses do
        local monster = Monster(spawnedBosses[i])
        if monster and monster:getName():lower() == name then
            return true
        else
            spawnedBosses[i] = nil
        end
    end
    return false
end

local globalevent = GlobalEvent("SpawnRaid")

function globalevent.onThink(...)
    local table = raids[math.random(#raids)]

    if math.random(100) > table.chance then
        return true
    end

    if table.type == "BOSS" then
        if not isSpawned(table.monster:lower()) then
            local boss = Game.createMonster(table.monster, table.position, true, false)
            if boss then
                spawnedBosses[#spawnedBosses + 1] = boss:getId()
            end
            if table.message ~= nil then
                broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
            end
        end
    elseif table.type == "NORMAL" then
        for i = 1, table.count do
            local randomPosition = Position(math.random(table.fromPos.x, table.toPos.x), math.random(table.fromPos.y, table.toPos.y), table.fromPos.z)
            if type(table.monsters) == "string" then
                Game.createMonster(table.monsters, randomPosition, true, false)
            else
                Game.createMonster(table.monsters[math.random(#table.monsters)], randomPosition, true, false)
            end
        end
        if table.message ~= nil then
            broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
        end
    end
    return true
end

globalevent:interval(1800000) -- will be executed every 1000ms
globalevent:register()

and the error:
\data\scripts\globalevents\raids.lua:31: attempt to index local 'table' (a nil value)
Hi,
You have raids table with 5 indexs, when math.random return 5 as value, the index 5 don't exist... Because you only have: [1], [2], [3], [4] and [6]. The index [5] don't exist. Change to this, and will work...

Lua:
local raids = {
    -- Bosses
    [1]  = {type = "BOSS", position = Position(49, 172, 10), monster = "Demodras", message = "Demodras has spawned", chance = 3},
    [2]  = {type = "BOSS", position = Position(115, 88, 7), monster = "Grorlam", message = "Grorlam has spawned", chance = 2},
    [3]  = {type = "BOSS", position = Position(102, 41, 8), monster = "Rotworm Queen", message = "Rotworm Queen has spawned", chance = 2},

    -- Raids fromPos, toPos
    [4] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = {"minotaur", "minotaur guard", "minotaur mage", "minotaur archer"}, count = 50, message = "Minotaur assault incoming!", chance = 20},
    [5] = {type = "NORMAL", fromPos = Position(127, 35, 7), toPos = Position(184, 65, 7), monsters = "Feverish Citizen", count = 50, message = "Feverish Citizens have invaded the streets!", chance = 10},
}

local spawnedBosses = {}

local function isSpawned(name)
    for i = 1, #spawnedBosses do
        local monster = Monster(spawnedBosses[i])
        if monster and monster:getName():lower() == name then
            return true
        else
            spawnedBosses[i] = nil
        end
    end
    return false
end

local globalevent = GlobalEvent("SpawnRaid")

function globalevent.onThink(...)
    local table = raids[math.random(#raids)]

    if math.random(100) > table.chance then
        return true
    end

    if table.type == "BOSS" then
        if not isSpawned(table.monster:lower()) then
            local boss = Game.createMonster(table.monster, table.position, true, false)
            if boss then
                spawnedBosses[#spawnedBosses + 1] = boss:getId()
            end
            if table.message ~= nil then
                broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
            end
        end
    elseif table.type == "NORMAL" then
        for i = 1, table.count do
            local randomPosition = Position(math.random(table.fromPos.x, table.toPos.x), math.random(table.fromPos.y, table.toPos.y), table.fromPos.z)
            if type(table.monsters) == "string" then
                Game.createMonster(table.monsters, randomPosition, true, false)
            else
                Game.createMonster(table.monsters[math.random(#table.monsters)], randomPosition, true, false)
            end
        end
        if table.message ~= nil then
            broadcastMessage(table.message, MESSAGE_EVENT_ADVANCE)
        end
    end
    return true
end

globalevent:interval(1800000) -- will be executed every 1000ms
globalevent:register()
 
Back
Top