• 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 add loot corpse monster

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil


I'm trying to adapt the script above so that instead of the items go to the player, in which case I would add the item to the animal's body, could you help me?

Lua:
local extra_loot = {
    {hasName = {"infernal demon", "infernal phantom", "branchy crawler", "rotten golem", "vibrant phantom", "courage leech", "cloak of terror", "turbulent elemental", "bony sea devil", "capricious phantom", "brachiodemon", "knight's apparition", "sorcerer's apparition", "paladin's apparition", "mould phantom"}, items = {
        {id = 38944, count = 1, chance = 80000}, -- 4%
    }}
}

function onDeath(monster, corpse, killer, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)   
    if not monster:isMonster() or monster:getMaster() then
        return true
    end
    
 
    if not corpse then
        return false
    end
    
    local owner = mostdamagekiller
    if killer and killer:isPlayer() then
        owner = killer
    end
    
    local player = Player(owner)

    if corpse and corpse:isContainer() then
        for i = 1, #extra_loot do
            corpse:addExtraLoot(player, monster, extra_loot[i])
        end
    end
    return true
end

Lua:
function Container:addExtraLoot(self, c, t)
    if t.hasName then       
        local cn = c:getName():lower()
        local cm = t.hasName       
        if not isInArray(cm, cn) then
            return true
        end
    end
    
    local mType = c:getType()
    
    local monsterLoot = mType:getLoot()

    
    
    for i = 1, #t.items do
        local count = 1
        if t.items[i].count then
            if t.items[i].countMax then
                count = math.random(t.items[i].count, t.items[i].countMax)
            else
                count = t.items[i].count
            end
        else
            if t.items[i].countMax then
                count = math.random(1, t.items[i].countMax)
            end
        end
        
        if math.random(0, 100000) <= t.items[i].chance then
            --self:addItem(t.items[i].id, count)
        end
    end
end
 
if you use TFS 1.3 oficial you can use this code:

example: data/scripts/scriptname.lua
Lua:
local monsters = {
    ["Demon"] = {
        { itemId = 2160, count = 100, chance = 60 }, -- 100 crystal coins
        { itemId = 2152, count = 100, chance = 100 } -- 100 platinum coins
    },
    ["Rat"] = {
        { itemId = 2160, count = 10, chance = 90 } -- 10 crystal coins
    }
}

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    local extraLoot = monsters[monster:getName()]
    if extraLoot then
        for _, it in pairs(extraLoot) do
            if it.chance >= math.random(1, 100) then
                corpse:addItem(it.itemId, it.count)
            end
        end
    end
end

ec:register(-1)
 
@Sarah Wesker A little question, how can I set this up to give the same item id, count, and chance to lots of monsters?
Good script btw, very usefull for something I need 😁 Here's example:

Code:
local monsters= { ["Demon"], ["Rat"], ["Rabbit"], ["Troll"] = {
{ itemId = 8300, count = 1, chance = 60}}

Or
Code:
{"Demon", "Rat", "Troll", "Rabbit", "etc"}
 
@Sarah Wesker A little question, how can I set this up to give the same item id, count, and chance to lots of monsters?
Good script btw, very usefull for something I need 😁 Here's example:

Code:
local monsters= { ["Demon"], ["Rat"], ["Rabbit"], ["Troll"] = {
{ itemId = 8300, count = 1, chance = 60}}
Lua:
local monsters = {
    [{"Demon", "Rat", "Cave Rat", "Mega Rat", "Ugly Rat", "LOL Rat"}] = {
        { itemId = 2160, count = 50, chance = 90 }, -- 100 crystal coins
        { itemId = 2152, count = 50, chance = 90 } -- 100 platinum coins
    },
    [{"Baby Demon", "Beautiful Demon", "Happy Demon", "My Demon"}] = {
        { itemId = 2160, count = 100, chance = 45 }, -- 100 crystal coins
        { itemId = 2152, count = 100, chance = 45 } -- 100 platinum coins
    }
}

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    local monsterName = monster:getName()
    for list, loot in pairs(monsters) do
        if table.contains(list, monsterName) then
            for _, it in pairs(loot) do
                if it.chance >= math.random(1, 100) then
                    corpse:addItem(it.itemId, it.count)
                end
            end
            return
        end
    end
end

ec:register(-1)
 
Lua:
local monsters = {
    [{"Demon", "Rat", "Cave Rat", "Mega Rat", "Ugly Rat", "LOL Rat"}] = {
        { itemId = 2160, count = 50, chance = 90 }, -- 100 crystal coins
        { itemId = 2152, count = 50, chance = 90 } -- 100 platinum coins
    },
    [{"Baby Demon", "Beautiful Demon", "Happy Demon", "My Demon"}] = {
        { itemId = 2160, count = 100, chance = 45 }, -- 100 crystal coins
        { itemId = 2152, count = 100, chance = 45 } -- 100 platinum coins
    }
}

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    local monsterName = monster:getName()
    for list, loot in pairs(monsters) do
        if table.contains(list, monsterName) then
            for _, it in pairs(loot) do
                if it.chance >= math.random(1, 100) then
                    corpse:addItem(it.itemId, it.count)
                end
            end
            return
        end
    end
end

ec:register(-1)
I didn't read well the post before, this is for official TFS 1.3, so i'm getting this error
I also registered it on creature.lua like the script reference of darkmu. Using TFS 1.2

Lua:
function Creature:onTargetCombat(target)
target:registerEvent("DodgeCritical")
    if not self then
        return true
    end
    if self:isPlayer() and target:isMonster() then  ----- here
        target:registerEvent("extra_loot_d") ----- here
    end

    if target:isPlayer() then
        if self:isMonster() then
            local protectionStorage = target:getStorageValue(Storage.combatProtectionStorage)

error.png
 
I didn't read well the post before, this is for official TFS 1.3, so i'm getting this error
I also registered it on creature.lua like the script reference of darkmu. Using TFS 1.2

Lua:
function Creature:onTargetCombat(target)
target:registerEvent("DodgeCritical")
    if not self then
        return true
    end
    if self:isPlayer() and target:isMonster() then  ----- here
        target:registerEvent("extra_loot_d") ----- here
    end

    if target:isPlayer() then
        if self:isMonster() then
            local protectionStorage = target:getStorageValue(Storage.combatProtectionStorage)

View attachment 57343

this is not a "regular" script, its a revscript/eventcallback type, just create a Lua file inside data/scripts folder and place it, there is no need to do anything else
 
this is not a "regular" script, its a revscript/eventcallback type, just create a Lua file inside data/scripts folder and place it, there is no need to do anything else
I know, sorry for expressing myself bad, that's why I said I didn't read well the post before. But I don't have scripts folder, I asked the post before as request for 1.2, only if it's possible, ofc. I will be very gratefull ☺️
 
Last edited:
I know, sorry for expressing myself bad, that's why I said I didn't read well the post before. But I don't have scripts folder, I asked the post before as request for 1.2, only if it's possible, ofc. I will be very gratefull ☺️
1.2 doesn't have loot system handled in Lua so you are stuck with using onDeath event :/
 
1.2 doesn't have loot system handled in Lua so you are stuck with using onDeath event :/
I'm very sorry, how I did not check that I was asking exactly the same script darkmu posted in the begining of the thread
I am a little off focus, my apologies for diverting the subject. Anyways, good to know there's a change in the loot system, i'll check to see which difference it has with 1.2. Regards!
 
Last edited:
I'm very sorry, how I did not check that I was asking exactly the same script darkmu posted in the begining of the thread
I am a little off focus, my apologies for diverting the subject. Anyways, good to know there's a change in the loot system, i'll check to see which difference it has with 1.2. Regards!
data/scripts/extraloot.lua
Lua:
local monsters = {
    [{"Demon", "Rat", "Cave Rat", "Mega Rat", "Ugly Rat", "LOL Rat"}] = {
        { itemId = 2160, count = 50, chance = 90 }, -- 100 crystal coins
        { itemId = 2152, count = 50, chance = 90 } -- 100 platinum coins
    },
    [{"Baby Demon", "Beautiful Demon", "Happy Demon", "My Demon"}] = {
        { itemId = 2160, count = 100, chance = 45 }, -- 100 crystal coins
        { itemId = 2152, count = 100, chance = 45 } -- 100 platinum coins
    }
}

local event = CreatureEvent("extraLootMonsters")

function event.onDeath(creature, corpse, lastHitKiller, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
    if not corpse then
        return true
    end
    local monsterName = creature:getName()
    for list, loot in pairs(monsters) do
        if table.contains(list, monsterName) then
            for _, it in pairs(loot) do
                if it.chance >= math.random(1, 100) then
                    corpse:addItem(it.itemId, it.count)
                end
            end
            return
        end
    end
    return true
end

event:register()

data/events/scripts/creature.lua
Lua:
local monsters = {
    "Demon",
    "Rat",
    "Cave Rat",
    "Mega Rat",
    "Ugly Rat",
    "LOL Rat",
    "Baby Demon",
    "Beautiful Demon",
    "Happy Demon",
    "My Demon"
}

-- add in function Creature:onTargetCombat
    if target and table.contains(monsters, target:getName()) then
        target:registerEvent("extraLootMonsters")
    end
 
Lua:
local monsters = {
    [{"Demon", "Rat", "Cave Rat", "Mega Rat", "Ugly Rat", "LOL Rat"}] = {
        { itemId = 2160, count = 50, chance = 90 }, -- 100 crystal coins
        { itemId = 2152, count = 50, chance = 90 } -- 100 platinum coins
    },
    [{"Baby Demon", "Beautiful Demon", "Happy Demon", "My Demon"}] = {
        { itemId = 2160, count = 100, chance = 45 }, -- 100 crystal coins
        { itemId = 2152, count = 100, chance = 45 } -- 100 platinum coins
    }
}

local ec = EventCallback

function ec.onDropLoot(monster, corpse)
    local monsterName = monster:getName()
    for list, loot in pairs(monsters) do
        if table.contains(list, monsterName) then
            for _, it in pairs(loot) do
                if it.chance >= math.random(1, 100) then
                    corpse:addItem(it.itemId, it.count)
                end
            end
            return
        end
    end
end

ec:register(-1)

I was a little confused, is this onDropLoot function the same function as Events? In case I am not using revscripts on my server.
 
Back
Top