• 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 - Drop all gold upon death

Joined
Dec 13, 2015
Messages
45
Reaction score
13
I'm trying to make it so that when the player dies, it will drop all the gold he is carrying (even with aol and/or full blessings).

So the file that takes care of this is droploot.lua, as you can see in the comments inside the code, I edited it.
The idea is pretty simple, but its my first try at making something up all by myself. So be nice please.

creaturescripts/scripts/others/droploot.lua:
Code:
function onDeath(player, corpse, killer, mostDamage, unjustified, mostDamage_unjustified)
    if getPlayerFlagValue(player, PlayerFlag_NotGenerateLoot) or player:getVocation():getId() == VOCATION_NONE then
        return true
    end
  
    -- Upon death player drops all the money he is carrying
    -- THIS IS WHAT I ADDED
    local coins = player:getMoney()
    player:removeMoney(coins)
    while coins > 10000 do
        local cc = cc + 1
        coins = coins - 10000
    end
    while coins > 100 do
        local pc = pc + 100
        coins = coins - 100
    end
    while coins > 0 do
        local gc = gc + 1
        coins = coins - 1
    end
    ContainerAddItem(corpse, 2160, cc)
    ContainerAddItem(corpse, 2152, pc)
    ContainerAddItem(corpse, 2148, gc)
    --  Nothing else was changed after this line
  
    local amulet = player:getSlotItem(CONST_SLOT_NECKLACE)
    if amulet and amulet.itemid == ITEM_AMULETOFLOSS and not isInArray({SKULL_RED, SKULL_BLACK}, player:getSkull()) then
        local isPlayer = false
        if killer then
            if killer:isPlayer() then
                isPlayer = true
            else
                local master = killer:getMaster()
                if master and master:isPlayer() then
                    isPlayer = true
                end
            end
        end

        if not isPlayer or not player:hasBlessing(6) then
            player:removeItem(ITEM_AMULETOFLOSS, 1, -1, false)
        end
    else
        for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
            local item = player:getSlotItem(i)
            if item then
                if isInArray({SKULL_RED, SKULL_BLACK}, player:getSkull()) or math.random(item:isContainer() and 100 or 1000) <= player:getLossPercent() then
                    item:moveTo(corpse)
                end
            end
        end
    end

    if not player:getSlotItem(CONST_SLOT_BACKPACK) then
        player:addItem(ITEM_BAG, 1, false, CONST_SLOT_BACKPACK)
    end
    return true
end

I don't have my server up atm to test it myself.
Is this gonna work? Is it performace inneficient?

Thanks for your help.
 
Last edited:
The while loops are unnecessary and will be a performance hit;
Use a little smarter logic and use single IF statements :)

Converted to single IF statements:
Code:
if coins >= 10000 then
    local cc = math.floor(coins / 10000)
    coins = coins - (cc * 10000)
    corpse:addItem(2160, cc)
end
if coins >= 100 then
    local pc = math.floor(coins / 100)
    coins = coins - (pc * 100)
    corpse:addItem(2152, pc)
end
if coins > 0 then
    local gc = coins
    corpse:addItem(2148, gc)
end


Use the LUA DEMO and execute the following code for verification/testing/learning:
Code:
coins = 1234567 -- change me to whatever for testing values
print("Coins: " .. coins .. "\r\n")

if coins >= 10000 then
    local cc = math.floor(coins / 10000)
    coins = coins - (cc * 10000)
    print("Crystal Coins: " .. cc)
end
if coins >= 100 then
    local pc = math.floor(coins / 100)
    coins = coins - (pc * 100)
    print("Platinum Coins: " .. pc)
end
if coins > 0 then
    local gc = coins
    print("Gold Coins: " .. gc)
end

I really like this idea, going to use it myself! TY.
 
Last edited:
The while loops are unnecessary, use a little smarter logic and use single IF statements :)

Converted to single IF statements:
Code:
if coins >= 10000 then
    local cc = math.floor(coins / 10000)
    coins = coins - (cc * 10000)
    ContainerAddItem(corpse, 2160, cc)
end
if coins >= 1000 then
    local pc = math.floor(coins / 100)
    coins = coins - (pc * 100)
    ContainerAddItem(corpse, 2152, pc)
end
if coins >= 0 then
    local gc = coins
    ContainerAddItem(corpse, 2148, gc)
end


Use the LUA DEMO and execute the following code for verification/testing/learning:
Code:
coins = math.random(999999)
print("Coins: " .. coins .. "\r\n")

if coins >= 10000 then
    local cc = math.floor(coins / 10000)
    coins = coins - (cc * 10000)
    print("Crystal Coins: " .. cc)
end
if coins >= 1000 then
    local pc = math.floor(coins / 100)
    coins = coins - (pc * 100)
    print("Platinum Coins: " .. pc)
end
if coins >= 0 then
    local gc = coins
    print("Gold Coins: " .. gc)
end

I really like this idea, going to use it myself! TY.
Thanks dude!

Don't you hate how boring it is to kill players while always being 100% sure that you won't get any loot?

Btw, is there any chance you could help me in this other post? The topic is almost the same.
https://otland.net/threads/tfs-1-2-player-has-a-chance-to-drop-certain-items-upon-death.243549/
 
Back
Top