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

TFS 1.X+ Help with onDeath creaturescript function crashing the server

Siegh

Thronar Developer
Joined
Mar 12, 2011
Messages
1,186
Solutions
1
Reaction score
510
Location
Brazil
I'm having a problem with this script. I've tried doing similarly as an onKill too but I ended up with the same issue. It works fine when I test it, but when I kill a lot of these monsters at the same time, the server crashes.
Code:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
chance = math.random(1,100)
timer = math.random(2000,4000)
pos = creature:getPosition()
    if chance <= 20 then
        addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
        addEvent(Game.createMonster, timer, "Skeleton Minion", pos)
        addEvent(doSendMagicEffect, timer, pos, 60)
        local function removeMinion()
            corpse:remove(1)
        end
        addEvent(removeMinion, timer)
    else
        return false
    end
end

Any possible reason and/or fix?
 
Solution
I'm having a problem with this script. I've tried doing similarly as an onKill too but I ended up with the same issue. It works fine when I test it, but when I kill a lot of these monsters at the same time, the server crashes.
Code:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
chance = math.random(1,100)
timer = math.random(2000,4000)
pos = creature:getPosition()
    if chance <= 20 then
        addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
        addEvent(Game.createMonster, timer, "Skeleton Minion", pos)
        addEvent(doSendMagicEffect, timer, pos, 60)
        local function removeMinion()
            corpse:remove(1)
        end...
I'm having a problem with this script. I've tried doing similarly as an onKill too but I ended up with the same issue. It works fine when I test it, but when I kill a lot of these monsters at the same time, the server crashes.
Code:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
chance = math.random(1,100)
timer = math.random(2000,4000)
pos = creature:getPosition()
    if chance <= 20 then
        addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
        addEvent(Game.createMonster, timer, "Skeleton Minion", pos)
        addEvent(doSendMagicEffect, timer, pos, 60)
        local function removeMinion()
            corpse:remove(1)
        end
        addEvent(removeMinion, timer)
    else
        return false
    end
end

Any possible reason and/or fix?
addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
This line is the issue.
You cannot send a full creature info into an addEvent.
It's unsafe, and can crash the server, as you've found out.
If you need to use a creature again in the future, you should transfer it's creatureId to the function, and re-create the creature, to ensure it still exists.

something like this
Lua:
addEvent(functionName, timer, creature:getId(), "<Clink, Clank>")
Lua:
local function functionName(creatureId, text)
    local creature = Creature(creatureId)
    if not creature then
        return false
    end

    creature:say(text, TALKTYPE_ORANGE_1)
    return true
end

-- Edit

kind of a poor example tho, since it's almost 100% guaranteed to return false, since the creature dying will no longer exist, and the orange text will never show.

I'd suggest combining your 3-4 addEvents into a single function that triggers.
That way the new creature you're summoning could be the one saying the text.
 
Last edited:
addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
This line is the issue.
You cannot send a full creature info into an addEvent.
It's unsafe, and can crash the server, as you've found out.
If you need to use a creature again in the future, you should transfer it's creatureId to the function, and re-create the creature, to ensure it still exists.

something like this
Lua:
addEvent(functionName, timer, creature:getId(), "<Clink, Clank>")
Lua:
local function functionName(creatureId, text)
    local creature = Creature(creatureId)
    if not creature then
        return false
    end

    creature:say(text, TALKTYPE_ORANGE_1)
    return true
end
Oh, so if the orange message is the reason, I assume I can just live without it. So by removing it, the crashes are dealt with?

I can alternatively use a different function to send the message at the appropriate position instead of being said by the creature.
 
Oh, so if the orange message is the reason, I assume I can just live without it. So by removing it, the crashes are dealt with?

I can alternatively use a different function to send the message at the appropriate position instead of being said by the creature.
More then likely, yes.
 
I'm having a problem with this script. I've tried doing similarly as an onKill too but I ended up with the same issue. It works fine when I test it, but when I kill a lot of these monsters at the same time, the server crashes.
Code:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
chance = math.random(1,100)
timer = math.random(2000,4000)
pos = creature:getPosition()
    if chance <= 20 then
        addEvent(doCreatureSay, timer, creature, "<Clink, Clank>", TALKTYPE_ORANGE_1)
        addEvent(Game.createMonster, timer, "Skeleton Minion", pos)
        addEvent(doSendMagicEffect, timer, pos, 60)
        local function removeMinion()
            corpse:remove(1)
        end
        addEvent(removeMinion, timer)
    else
        return false
    end
end

Any possible reason and/or fix?
Lua:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local chance = math.random(1, 100)
    if chance <= 20 then
        local timer = math.random(2000, 4000)
        local pos = creature:getPosition()
        addEvent(function(pos)
            local p = Game.getPlayers()[1]
            if p then
                p:say("<Clink, Clank>", TALKTYPE_ORANGE_1, false, nil, pos)
            end
        end, timer, pos)
        addEvent(function(corpseId, pos)
            local tile = Tile(pos)
            if tile then
                local corpse = tile:getItemById(corpseId)
                if corpse then
                    corpse:remove()
                    Game.createMonster("Skeleton Minion", pos)
                end
            end
        end, timer, corpse:getId(), pos)
    end
    return true
end
 
Solution
Lua:
function onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    local chance = math.random(1, 100)
    if chance <= 20 then
        local timer = math.random(2000, 4000)
        local pos = creature:getPosition()
        addEvent(function(pos)
            local p = Game.getPlayers()[1]
            if p then
                p:say("<Clink, Clank>", TALKTYPE_ORANGE_1, false, nil, pos)
            end
        end, timer, pos)
        addEvent(function(corpseId, pos)
            local tile = Tile(pos)
            if tile then
                local corpse = tile:getItemById(corpseId)
                if corpse then
                    corpse:remove()
                    Game.createMonster("Skeleton Minion", pos)
                end
            end
        end, timer, corpse:getId(), pos)
    end
    return true
end
Works perfectly, thanks :)
 
Back
Top