• 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.2 onPrepareDeath

Itutorial

Legendary OT User
Joined
Dec 23, 2014
Messages
2,435
Solutions
68
Reaction score
1,083
Can someone tell me why this script isn't working by chance?

Code:
local monsters = {
["Jellyfish"] = {level_max = 10, exp = 2000},
["Flymuh"] = {level_max = 20, exp = 2000},
["Mutated Crab"] = {level_max = 30, exp = 15000},
["Octapus"] = {level_max = 50, exp = 25000},
["Hydromancer"] = {level_max = 60, exp = 30000},
["Shark King"] = {level_max = 70, exp = 40000},
["Aquasin"] = {level_max = 80, exp = 50000}
}

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

function onPrepareDeath(creature, deathList)
if isMonster(creature) then
        MONS = monsters[creature:getName()]
       
        if MONS then
            for i, v in ipairs(deathList) do
                KILLER = Player(deathList[v])
               
                if KILLER:getLevel() < MONS.level_max then
                    KILLER:addExperience(getExpForLevel(KILLER:getLevel() + 1) - KILLER:getExperience())
                else
                    KILLER:addExperience(MONS.exp)
                end
            end
        end
end
return true
end
 
There is no deathlist in TFS 1+
you should use like;
function onPrepareDeath(creature, killer)
local deathList = creature:getDamageMap()
 
just edit those lines to mine
Code:
local monsters = {
["Jellyfish"] = {level_max = 10, exp = 2000},
["Flymuh"] = {level_max = 20, exp = 2000},
["Mutated Crab"] = {level_max = 30, exp = 15000},
["Octapus"] = {level_max = 50, exp = 25000},
["Hydromancer"] = {level_max = 60, exp = 30000},
["Shark King"] = {level_max = 70, exp = 40000},
["Aquasin"] = {level_max = 80, exp = 50000}
}

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

function onPrepareDeath(creature, killer)
local deathList = creature:getDamageMap()
if isMonster(creature) then
        MONS = monsters[creature:getName()]
      
        if MONS then
            for i, v in ipairs(deathList) do
                KILLER = Player(deathList[v])
              
                if KILLER:getLevel() < MONS.level_max then
                    KILLER:addExperience(getExpForLevel(KILLER:getLevel() + 1) - KILLER:getExperience())
                else
                    KILLER:addExperience(MONS.exp)
                end
            end
        end
end
return true
end
 
Alright, I put that in but nothing happens. Any ideas why?

My xml code

Code:
<event type="preparedeath" name="levelBlock" script="levelblock.lua" />
 
Last edited by a moderator:
Alright, I put that in but nothing happens. Any ideas why?

My xml code

Code:
<event type="preparedeath" name="levelBlock" script="levelblock.lua" />
I think it has to be prepareDeath, you also have to register it in data/creaturescripts/scripts/login.lua, like:
Code:
-- Events
player:registerEvent("levelBlock")
 
I tried all that. I know my way around TFS...just having a problem figuring out how to get the script to register all of the players attacking the monster before it died.
 
I tried all that. I know my way around TFS...just having a problem figuring out how to get the script to register all of the players attacking the monster before it died.
So, what you mean with 'nothing happens'? Do the script even runs? Have you tried to debug it? Something like:
Code:
function onPrepareDeath(creature, killer)
    local deathList = creature:getDamageMap()

    if not isMonster(creature) then
        print('is not a monster')

        return false
    end

    MONS = monsters[creature:getName()]

    if not MONS then
        print('not MONS')

        return false
    end

    print('lets go')

    for i, v in ipairs(deathList) do
        -- Do even more prints here

        KILLER = Player(deathList[v])

        if KILLER:getLevel() < MONS.level_max then
            KILLER:addExperience(getExpForLevel(KILLER:getLevel() + 1) - KILLER:getExperience())
        else
            KILLER:addExperience(MONS.exp)
        end
    end

    return true
end
 
So the script is stopping at the loop. The loop doesn't give an error so I am not sure what to do from here.

Code:
for i, v in ipairs(deathList) do
--after this nothing no prints work--
 
Maybe I didn't say it clearly. After the loop the script doesn't read anything. It stops at the loop with no errors. I have tried changing the loops to many different ways.
 
Why are you even using onPrepareDeath? That is executed before death, which means if you want to stop certain death. Asfar i understand of your script, you want the monster to die and fetch all attackers data.


Code:
local config = {
    ["Jellyfish"] = {maxLevel = 10, exp = 2000},
    ["Flymuh"] = {maxLevel = 20, exp = 2000},
    ["Mutated Crab"] = {maxLevel = 30, exp = 15000},
    ["Octapus"] = {maxLevel = 50, exp = 25000},
    ["Hydromancer"] = {maxLevel = 60, exp = 30000},
    ["Shark King"] = {maxLevel = 70, exp = 40000},
    ["Aquasin"] = {maxLevel = 80, exp = 50000}
}

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

function onDeath(monster, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local targetMonster = config[monster:getName()]
    if targetMonster then
        return true
    end

    for id in pairs(monster:getDamageMap()) do
        local player = Player(id)
        if player then
            if player:getLevel() < targetMonster.maxLevel then
                player:addExperience(getExpForLevel(player:getLevel() + 1) - player:getExperience())
            else
                player:addExperience(targetMonster.experience)
            end
        end
    end

    return true
end

Dont forgot to register the event on the monsters.
 
Last edited:
Thank you printer. The reason I used prepare death is because I was checking the playerDeath file in the server and it was onDeath(player, x, x,x) so I assumed it was only meant for players. Thank you for clearing that up for me. You are the best :)
 
Btw everyone....When making a onDeath script or anything in creaturescripts you CANNOT use return true or return false...It will stop the script wherever you put it. Here is the offical working script:

Code:
local config = {
    ["Jellyfish"] = {maxLevel = 10, exp = 2000},
    ["Flymuh"] = {maxLevel = 20, exp = 2000},
    ["Mutated Crab"] = {maxLevel = 30, exp = 15000},
    ["Octapus"] = {maxLevel = 50, exp = 25000},
    ["Hydromancer"] = {maxLevel = 60, exp = 30000},
    ["Shark King"] = {maxLevel = 70, exp = 40000},
    ["Aquasin"] = {maxLevel = 80, exp = 50000}
}

local function getExpForLevel(level)
    level = level - 1
    return ((50 * level * level * level) - (150 * level * level) + (400 * level)) / 3
end

function onDeath(monster, corpse, killer, mostDamageKiller, unjustified, mostDamageUnjustified)
    local targetMonster = config[monster:getName()]
    if targetMonster then

    for id in pairs(monster:getDamageMap()) do
        local player = Player(id)
        if player then
            if player:getLevel() < targetMonster.maxLevel then
                player:addExperience(getExpForLevel(target:getLevel() + 1), false)
            else
                player:addExperience(targetMonster.exp)
            end
        end
    end
    end

    return true
end
 

Similar threads

Back
Top