• 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 player loot

Sun

Knowledge is power - France is bacon
Joined
Jan 26, 2015
Messages
334
Solutions
22
Reaction score
249
Using tfs 0.4
Been trying to workout a script that makes item xxxx appear in the players corpse when they die.
I found a script which supposedly works for tfs 1.0
I've wasted a lot of time trying to adjust it to work for 0.4 but I honestly don't know which functions to use, or if it should be registered in login.lua(everytime I do it the corpse doesn't even appear).

Code:
function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    local container = Container(corpse)
    if not Player(killer) or not container then
        return
    end

    container:addItem(math.random(100) > 50 and 2671 or 2229, 1)
end

I'd really appreciate if someone could help me with this.
Ty in advance //Cade
 
Code:
function onDeath(cid, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    if not isContainer(corpse.uid) or not isPlayer(killer) then
        return false
    end
    doAddContainerItem(corpse.uid, math.random(100) > 50 and 2671 or 2229, 1)
end
 
Actually that might not work as expected, let me re-write it.

Code:
function onDeath(cid, corpse, deathList)
    if not isContainer(corpse.uid) then
        return false
    end
    if type(deathList) == 'table' then
        for _, killer in pairs(deathList) do
            if isPlayer(killer) then
                return doAddContainerItem(corpse.uid, math.random(100) > 50 and 2671 or 2229, 1)
            end
        end
    else
        return isPlayer(deathList) and doAddContainerItem(corpse.uid, math.random(100) > 50 and 2671 or 2229, 1) or false
    end
    return false
end
 
Last edited:
Thank a bunch!

Could you explain this line?
if type(deathList) == 'table' then
 
Thank a bunch!

Could you explain this line?
if type(deathList) == 'table' then
According to the sources, deathList is a table, the predefined function type returns a string of the datatype of the argument passed in this case deathList.
If we didn't check to make sure deathList was a table then an error would be thrown in the for loop if deathList was not a table.

The else clause just handles deathList as if it were a not a table

The return false at the end of the interface although could have been set inside of if type etc.. is important incase the killer is not a player.
 
According to the sources, deathList is a table, the predefined function type returns a string of the datatype of the argument passed in this case deathList.
If we didn't check to make sure deathList was a table then an error would be thrown in the for loop if deathList was not a table.

The else clause just handles deathList as if it were a not a table

The return false at the end of the interface although could have been set inside of if type etc.. is important incase the killer is not a player.

Hmm, But if deathList is a table, wouldn't we only need to check it in the test fase?
wouldn't the actual script work just as good like this:

Code:
function onDeath(cid, corpse, deathList)
    if not isContainer(corpse.uid) then
        return false
    end
    for _, killer in pairs(deathList) do
        if isPlayer(killer) then
            return doAddContainerItem(corpse.uid, math.random(100) > 50 and 2671 or 2229, 1)
        end
    end
    return false
end
 
if deathList is not table then it will return error.
ERROR Message: "Im trying real hard looping a table here, but you gave me freaking bug to deal with, get your shit together PLX"
 
Hmm, But if deathList is a table, wouldn't we only need to check it in the test fase?
wouldn't the actual script work just as good like this:

Code:
function onDeath(cid, corpse, deathList)
    if not isContainer(corpse.uid) then
        return false
    end
    for _, killer in pairs(deathList) do
        if isPlayer(killer) then
            return doAddContainerItem(corpse.uid, math.random(100) > 50 and 2671 or 2229, 1)
        end
    end
    return false
end
Sure, I was just accounting for deathList not being a table :)
if deathList is not table then it will return error.
ERROR Message: "Im trying real hard looping a table here, but you gave me freaking bug to deal with, get your shit together PLX"
What are you talking about?
 
Back
Top