• 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 onDeath error

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
why?

Lua:
Lua Script Error: [Main Interface]
in a timer event called from:
data/creaturescripts/scripts/Feyrist/Feyrist7.lua:onDeath
attempt to index a nil value
stack traceback:
    [C]: at 0x00571f00
    [C]: in function 'createMonster'
    data/creaturescripts/scripts/Feyrist/Feyrist7.lua:38: in function <data/creaturescripts/scripts/Feyrist/Feyrist7.lua:37>

script:

Lua:
local function criarFeyrist(PositionBorn)
    local monstro = Game.createMonster("Faun", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

local function criarFeyrist2(PositionBorn)
    local monstro = Game.createMonster("Pooka", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

local function criarFeyrist3(PositionBorn)
    local monstro = Game.createMonster("Nymph", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

local function criarFeyrist4(PositionBorn)
    local monstro = Game.createMonster("Dark Faun", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

local function criarFeyrist5(PositionBorn)
    local monstro = Game.createMonster("Twisted Pooka", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

local function criarFeyrist6(PositionBorn)
    local monstro = Game.createMonster("Boogy", PositionBorn, false, true)
    if monstro then
    monstro:setSpawnPoint(PositionBorn)
    monstro:registerEvent("Feyrist7")
    monstro:registerEvent("FeyristThink")
    end
end

function onDeath(creature)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end
    --local PositionBorn = targetMonster:getSpawnPoint()   
    -- se for dia
    if isTwilight() or isDay() then
        if targetMonster:getName():lower() == 'faun' or targetMonster:getName():lower() == 'dark faun' then
            local PositionBorn = targetMonster:getSpawnPoint()
            addEvent(criarFeyrist, 100 * 1000, PositionBorn) -- addEvent
           
        elseif targetMonster:getName():lower() == 'pooka' or targetMonster:getName():lower() == 'twisted pooka' then
            local PositionBorn = targetMonster:getSpawnPoint()
            addEvent(criarFeyrist2, 100 * 1000, PositionBorn) -- addEvent
           
        elseif targetMonster:getName():lower() == 'nymph' or targetMonster:getName():lower() == 'boogy' then
            local PositionBorn = targetMonster:getSpawnPoint()
            addEvent(criarFeyrist3, 100 * 1000, PositionBorn) -- addEvent
        end   
    else
    -- se for noite
        if targetMonster:getName():lower() == 'faun' or targetMonster:getName():lower() == 'dark faun' then
            local PositionBorn = targetMonster:getSpawnPoint()
            addEvent(criarFeyrist4, 100 * 1000, PositionBorn) -- addEvent
           
        elseif targetMonster:getName():lower() == 'pooka' or targetMonster:getName():lower() == 'twisted pooka' then
            local PositionBorn = targetMonster:getSpawnPoint()
            addEvent(criarFeyrist5, 100 * 1000, PositionBorn) -- addEvent
           
        elseif targetMonster:getName():lower() == 'nymph' or targetMonster:getName():lower() == 'boogy' then
            local PositionBorn = targetMonster:getSpawnPoint()   
            addEvent(criarFeyrist6, 100 * 1000, PositionBorn) -- addEvent
           
        end
    end
    return true
end
 
Solution
Create a local function and call.

Lua:
local config = {
    nightTime = {
        monsters = {
           ["twisted pooka"] = {toMonster = 'Twisted Pooka'},
            ["dark faun"] = {toMonster = 'Faun'},
        },
    },
    daylight = {
        monsters = {
             ["pooka"] = {toMonster = 'Twisted Pooka'},
            ["faun"] = {toMonster = 'Dark Faun'},
        },
    },
}

local function spawnCreature(monsterName, position)
    if monsterName ~= nil and position ~= nil then
        local monster = Game.createMonster(monsterName, position, false, true)
        if monster then
            monster:setSpawnPoint(position)
            monster:registerEvent("Feyrist7")
            monster:registerEvent("FeyristThink")
        end...
print PositionBorn before it's used
Game.createMonster will give you an error saying "attempt to index a nil value" when you use an invalid position as its spawnpoint
it's likely that targetMonster:getSpawnPoint() is returning nil, so in the addEvent you're also passing nil which leads to that error
 
Tem que declarar uma variavel para receber o monstro
Ex.: local funtion criarferyrist(position, creature)

but its declared
Lua:
(PositionBorn)

print PositionBorn before it's used
Game.createMonster will give you an error saying "attempt to index a nil value" when you use an invalid position as its spawnpoint
it's likely that targetMonster:getSpawnPoint() is returning nil, so in the addEvent you're also passing nil which leads to that error

Its seems PositionBorn is nil, but why its nil? if the monster was spawned before?
 
but its declared
Lua:
(PositionBorn)



Its seems PositionBorn is nil, but why its nil? if the monster was spawned before?
i have no idea, getSpawnPoint is part of your engine so i don't know what the code looks like
i'd assume the only way it would be nil is if the monster was spawned through Game.createMonster instead of a spawn
 
i have no idea, getSpawnPoint is part of your engine so i don't know what the code looks like
i'd assume the only way it would be nil is if the monster was spawned through Game.createMonster instead of a spawn

yes we created with Game.createMonster, but if the monster was created, how...? its wired

Lua:
local monsterStartPos = {}

function Monster.getStartPos(self)
    return monsterStartPos[self:getId()] or nil
end

function Monster.setStartPos(self, position)
    monsterStartPos[self:getId()] = position or self:getPosition()
end

local spawnPoints = {}

function Monster.setSpawnPoint(self, spawnPoint)
    spawnPoints[self:getId()] = spawnPoint or self:getPosition()
end

function Monster.getSpawnPoint(self)
    return spawnPoints[self:getId()] or nil
end
 
Lua:
local config = {
    nightTime = {
        monsters = {
           ["twisted pooka"] = {toMonster = 'Twisted Pooka'},
            ["dark faun"] = {toMonster = 'Faun'},
        },
    },

    daylight = {
        monsters = {
             ["pooka"] = {toMonster = 'Twisted Pooka'},
            ["faun"] = {toMonster = 'Dark Faun'},
        },
    },
}

function onDeath(creature)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end

    local monsterTable = nil
    if isTwilight() or isDay() then
        monsterTable = config.daylight.monsters[targetMonster:getName():lower()]
    else
        monsterTable = config.nightTime.monsters[targetMonster:getName():lower()]
    end

    if monsterTable ~= nil and type(monsterTable) == "table" then
        local monsterName = monsterTable.toMonster
        local position = targetMonster:getSpawnPoint()
        if not position then
            print(string.format("[Warning] - Cannot create %s. The position is pointed to nil.", monsterName))
            return true
        end

        local monster = Game.createMonster(monsterName, position, false, true)
        if monster then
            monster:setSpawnPoint(position)
            monster:registerEvent("Feyrist7")
            monster:registerEvent("FeyristThink")
        end
    end
end
 
Last edited:
Lua:
local config = {
    nightTime = {
        monsters = {
           ["twisted pooka"] = {toMonster = 'Twisted Pooka'},
            ["dark faun"] = {toMonster = 'Faun'},
        },
    },

    daylight = {
        monsters = {
             ["pooka"] = {toMonster = 'Twisted Pooka'},
            ["faun"] = {toMonster = 'Dark Faun'},
        },
    },
}

function onDeath(creature)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end

    local monsterTable = nil
    if isTwilight() or isDay() then
        monsterTable = config.daylight.monsters[targetMonster:getName():lower()]
    else
        monsterTable = config.nightTime.monsters[targetMonster:getName():lower()]
    end

    if monsterTable ~= nil and type(monsterTable) == "table" then
        local monsterName = monsterTable.toMonster
        local position = targetMonster:getSpawnPoint()
        if not position then
            print(string.format("[Warning] - Cannot create %s. The position is pointed to nil.", monsterName))
            return true
        end

        local monster = Game.createMonster(monsterName, position, false, true)
        if monster then
            monster:setSpawnPoint(position)
            monster:registerEvent("Feyrist7")
            monster:registerEvent("FeyristThink")
        end
    end
end

thanks man!
but we need addEvent, cuz if the monsters death, he can't spawn at the same time, understand?
u put a check if the position exists, so it will not appear error but this will not spawn the monster too
 
Last edited by a moderator:
Create a local function and call.

Lua:
local config = {
    nightTime = {
        monsters = {
           ["twisted pooka"] = {toMonster = 'Twisted Pooka'},
            ["dark faun"] = {toMonster = 'Faun'},
        },
    },
    daylight = {
        monsters = {
             ["pooka"] = {toMonster = 'Twisted Pooka'},
            ["faun"] = {toMonster = 'Dark Faun'},
        },
    },
}

local function spawnCreature(monsterName, position)
    if monsterName ~= nil and position ~= nil then
        local monster = Game.createMonster(monsterName, position, false, true)
        if monster then
            monster:setSpawnPoint(position)
            monster:registerEvent("Feyrist7")
            monster:registerEvent("FeyristThink")
        end
    end
end
function onDeath(creature)
    local targetMonster = creature:getMonster()
    if not targetMonster then
        return true
    end
    local monsterTable = nil
    if isTwilight() or isDay() then
        monsterTable = config.daylight.monsters[targetMonster:getName():lower()]
    else
        monsterTable = config.nightTime.monsters[targetMonster:getName():lower()]
    end
    if monsterTable ~= nil and type(monsterTable) == "table" then
        local monsterName = monsterTable.toMonster
        local position = targetMonster:getSpawnPoint()
        if not position then
            print(string.format("[Warning] - Cannot create %s. The position is pointed to nil.", monsterName))
            return true
        end
       
        addEvent(spawnCreature, 100 * 1000, monsterName, position)
    end
end
 
Solution
Back
Top