• 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+ onDrop console error.

OTcreator

Active Member
Joined
Feb 14, 2022
Messages
425
Solutions
1
Reaction score
44
Hi,
I have console error when i try check player storage on eventcallbacks/monsters/defaultOnDrop.lua

Why can not use storage function?

Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do

            -- // NEW CODE
            local bonus = player:getStorageValue(1111)
            if bonus < os.time() then
                local extraPercent = 20
                local default = monsterLoot[i].chance
         
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraPercent * (default / 1000) / 100) * 1000)
                end
            end
            -- // NEW CODE

            local item = corpse:createLootItem(monsterLoot[i])
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
            else
                player:sendTextMessage(MESSAGE_LOOT, text)
            end
        end
    else
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_LOOT, text)
        end
    end
end

ec:register()
 
Solution
Hi,
I have console error when i try check player storage on eventcallbacks/monsters/defaultOnDrop.lua

Why can not use storage function?

Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do

            -- // NEW CODE
            local bonus = player:getStorageValue(1111)
            if bonus < os.time() then
                local extraPercent = 20
                local default = monsterLoot[i].chance...
Hi,
I have console error when i try check player storage on eventcallbacks/monsters/defaultOnDrop.lua

Why can not use storage function?

Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do

            -- // NEW CODE
            local bonus = player:getStorageValue(1111)
            if bonus < os.time() then
                local extraPercent = 20
                local default = monsterLoot[i].chance
 
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraPercent * (default / 1000) / 100) * 1000)
                end
            end
            -- // NEW CODE

            local item = corpse:createLootItem(monsterLoot[i])
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
            else
                player:sendTextMessage(MESSAGE_LOOT, text)
            end
        end
    else
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
        else
            player:sendTextMessage(MESSAGE_LOOT, text)
        end
    end
end

ec:register()

You're logic is flawed in a lot of places.
Try this (untested, and edited on my mobile xD):
Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)

    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local mType = self:getType()
    local player = Player(corpse:getCorpseOwner())

    if not player or player:getStamina() > 840 then
        local chanceBonus = (player and player:getStorageValue(1111) < os.time()) and 20 or 0
   
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do
            local default = monsterLoot[i].chance
            if default < 100000 then
                monsterLoot[i].chance = math.min(default + ((chanceBonus  * (default / 1000) / 100) * 1000), 100000)
            end

            local item = corpse:createLootItem(monsterLoot[i])
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end   
    end

    if player then
        local text = player:getStamina() > 840
            and ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription())
            or ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())

        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
            return true
        end
       
        player:sendTextMessage(MESSAGE_LOOT, text)
    end
   
end

ec:register()
 
Last edited:
Solution
can't guarantee that player exists in this context.
There is a script in which the function checks if the player has a ring on, so also downloading storage should work?

Ring that increases loot rate TFS 1.3 (https://otland.net/threads/ring-that-increases-loot-rate-tfs-1-3.279665/)

1707652202693.png

This is my full script.

Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()
    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()
        for i = 1, #monsterLoot do
       
           -- // Loot bonus for players.
               local extra = math.random(1000)
            if extra <= 5 then  
                corpse:getPosition():sendMagicEffect(CONST_ME_YELLOWENERGY)
                corpse:getPosition():sendMagicEffect(CONST_ME_CRAPS)
                local extraThree = 500
                local default = monsterLoot[i].chance
               
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraThree * (default / 1000) / 100) * 1000)
                end
            end
           
            if player:getStorageValue(45396) > os.time() then
                local extraTwo = player:getStorageValue(45397)
                local default = monsterLoot[i].chance
           
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraTwo * (default / 1000) / 100) * 1000)
                end
            end
         
            if (os.date("%A")) == "Saturday" or (os.date("%A")) == "Sunday" then
                local extraOne = 50
                local default = monsterLoot[i].chance
           
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraOne * (default / 1000) / 100) * 1000)
                end
            end
           
            if player:getStorageValue(45396) < os.time() and player:getStorageValue(45397) > 1 then
                player:setStorageValue(45397, 0) -- set bonus 0% if not time.
            end
            -- // End bonus.
           
            local item = corpse:createLootItem(monsterLoot[i], self:getName())
            if not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse.')
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription() ..".")
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            else
                player:sendTextMessage(22, text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            end
        end
    else
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        else
            player:sendTextMessage(22, text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        end
    end
end

ec:register()
 
Last edited:
I fixed a few minor issues, and then added it to the print to check if the bonus was applied to the player or not. See the result that came out in the console... It seems to be working. I only tested one Rotworn.
Loot bonus activated for player.
Weekend bonus activated for player.

Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        print("Loot rate is 0. No loot will be dropped.")
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()

    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()

        for i = 1, #monsterLoot do
            -- Loot bonus for players.
            local extra = math.random(1000)
            if extra <= 5 then 
                print("Loot bonus activated for player.")
                corpse:getPosition():sendMagicEffect(CONST_ME_YELLOWENERGY)
                corpse:getPosition():sendMagicEffect(CONST_ME_CRAPS)
                local extraThree = 500
                local default = monsterLoot[i].chance
              
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraThree * (default / 1000) / 100) * 1000)
                end
            end

            if player:getStorageValue(45396) > os.time() then
                local extraTwo = player:getStorageValue(45397)
                local default = monsterLoot[i].chance
          
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraTwo * (default / 1000) / 100) * 1000)
                end
            end
        
            if (os.date("%A")) == "Saturday" or (os.date("%A")) == "Sunday" then
                print("Weekend bonus activated for player.")
                local extraOne = 50
                local default = monsterLoot[i].chance
          
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraOne * (default / 1000) / 100) * 1000)
                end
            end

            if player:getStorageValue(45396) < os.time() and player:getStorageValue(45397) > 1 then
                player:setStorageValue(45397, 0) -- set bonus 0% if not time.
            end
            -- End bonus.

            -- Attempt to create loot item
            local item = corpse:createLootItem(monsterLoot[i], self:getName())

            if type(item) == "boolean" and not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse for monsterLoot[' .. i .. '].')
            elseif type(item) == "userdata" then
                print("Loot dropped:", item:getName())
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription() ..".")
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            else
                player:sendTextMessage(22, text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            end
        end
    else
        print("No loot dropped due to low stamina.")
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        else
            player:sendTextMessage(22, text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        end
    end
end

ec:register()
 
I fixed a few minor issues, and then added it to the print to check if the bonus was applied to the player or not. See the result that came out in the console... It seems to be working. I only tested one Rotworn.


Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        print("Loot rate is 0. No loot will be dropped.")
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()

    if not player or player:getStamina() > 840 then
        local monsterLoot = mType:getLoot()

        for i = 1, #monsterLoot do
            -- Loot bonus for players.
            local extra = math.random(1000)
            if extra <= 5 then
                print("Loot bonus activated for player.")
                corpse:getPosition():sendMagicEffect(CONST_ME_YELLOWENERGY)
                corpse:getPosition():sendMagicEffect(CONST_ME_CRAPS)
                local extraThree = 500
                local default = monsterLoot[i].chance
            
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraThree * (default / 1000) / 100) * 1000)
                end
            end

            if player:getStorageValue(45396) > os.time() then
                local extraTwo = player:getStorageValue(45397)
                local default = monsterLoot[i].chance
        
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraTwo * (default / 1000) / 100) * 1000)
                end
            end
      
            if (os.date("%A")) == "Saturday" or (os.date("%A")) == "Sunday" then
                print("Weekend bonus activated for player.")
                local extraOne = 50
                local default = monsterLoot[i].chance
        
                if default < 100000 then
                    monsterLoot[i].chance = default + ((extraOne * (default / 1000) / 100) * 1000)
                end
            end

            if player:getStorageValue(45396) < os.time() and player:getStorageValue(45397) > 1 then
                player:setStorageValue(45397, 0) -- set bonus 0% if not time.
            end
            -- End bonus.

            -- Attempt to create loot item
            local item = corpse:createLootItem(monsterLoot[i], self:getName())

            if type(item) == "boolean" and not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse for monsterLoot[' .. i .. '].')
            elseif type(item) == "userdata" then
                print("Loot dropped:", item:getName())
            end
        end

        if player then
            local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription() ..".")
            local party = player:getParty()
            if party then
                party:broadcastPartyLoot(text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            else
                player:sendTextMessage(22, text)
                player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
            end
        end
    else
        print("No loot dropped due to low stamina.")
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        else
            player:sendTextMessage(22, text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        end
    end
end

ec:register()

Ok, but you have displayed printy for those items what works.
The console says line 27:
if player:getStorageValue(45396) > os.time() then

EDIT:

When i add this on code:
Lua:
if player:getStorageValue(45396) > os.time() then
                print("Daily bonus works.")

Print not visible in console.
 
Last edited:
I tested with the player without storage. See the result in the console.
Storage Value 45396: -1
Storage Value 45397: -1
Weekend bonus applied for player.
Total chance for loot: 50050
Storage Value 45396: -1
Storage Value 45397: -1
Weekend bonus applied for player.
Total chance for loot: 100050
Storage Value 45396: -1
Storage Value 45397: -1
Weekend bonus applied for player.
Total chance for loot: 50050
I tested the player with storage and see the results.
Weekend bonus applied for player.
Total chance for loot: 50050
Storage Value 45396: 1
Storage Value 45397: 1
Daily bonus applied for player.
Weekend bonus applied for player.
Total chance for loot: 50055
Storage Value 45396: 42
Storage Value 45397: 0
Weekend bonus applied for player.
Total chance for loot: 100050
Storage Value 45396: 42
Storage Value 45397: 0
Weekend bonus applied for player.
Total chance for loot: 50050
Did you see the value change? It was 50050 before and then it became 50055? I believe it's working, but I'm not sure. It's important to test the script I made.
Lua:
local ec = EventCallback

ec.onDropLoot = function(self, corpse)
    if configManager.getNumber(configKeys.RATE_LOOT) == 0 then
        print("Loot rate is 0. No loot will be dropped.")
        return
    end

    local player = Player(corpse:getCorpseOwner())
    local mType = self:getType()

    if not player or player:getStamina() <= 840 then
        print("No loot dropped due to low stamina.")
        local text = ("Loot of %s: nothing (due to low stamina)"):format(mType:getNameDescription())
        local party = player:getParty()
        if party then
            party:broadcastPartyLoot(text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        else
            player:sendTextMessage(22, text)
            player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
        end
        return
    end

    local monsterLoot = mType:getLoot()

    for i = 1, #monsterLoot do
        local bonusChance = 0

        -- Check storage values for daily bonus
        local dailyBonusTime = player:getStorageValue(45396) or 0
        local dailyBonusCount = player:getStorageValue(45397) or 0
        print("Storage Value 45396:", dailyBonusTime)
        print("Storage Value 45397:", dailyBonusCount)

        -- Check for daily bonus
        local currentTime = os.time()
        local currentDay = os.date("%j", currentTime)
        local lastDailyBonusDay = player:getStorageValue(45396) or 0

        if currentDay ~= lastDailyBonusDay and dailyBonusCount > 0 then
            bonusChance = bonusChance + 5
            player:setStorageValue(45397, dailyBonusCount - 1) -- Reduce daily bonus count after it's used
            player:setStorageValue(45396, currentDay) -- Store current day to track daily bonus usage
            print("Daily bonus applied for player.")
        end

        -- Check for weekend bonus
        local currentDayOfWeek = os.date("%A")
        if currentDayOfWeek == "Saturday" or currentDayOfWeek == "Sunday" then
            bonusChance = bonusChance + 50
            print("Weekend bonus applied for player.")
        end

        local totalChance = monsterLoot[i].chance + bonusChance
        print("Total chance for loot:", totalChance)

        if math.random(100) <= totalChance then
            local item = corpse:createLootItem(monsterLoot[i], self:getName())

            if type(item) == "boolean" and not item then
                print('[Warning] DropLoot:', 'Could not add loot item to corpse for monsterLoot[' .. i .. '].')
            elseif type(item) == "userdata" then
                print("Loot dropped:", item:getName())
            end
        else
            print("No loot dropped.")
        end
    end

    local text = ("Loot of %s: %s"):format(mType:getNameDescription(), corpse:getContentDescription() ..".")
    local party = player:getParty()
    if party then
        party:broadcastPartyLoot(text)
        player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
    else
        player:sendTextMessage(22, text)
        player:sendChannelMessage('', text, TALKTYPE_CHANNEL_O, 12)
    end
end

ec:register()
 
Last edited:
@OTcreator have you give a try to @Fjorda repply?
He seemed to completely ignore mine haha. But, the script he later posted is completely different to the one he originally posted, so I'm not going to waste my time rewriting his second one xD

@Mateus Robeerto There are still a few flaws in your script, such as player may not exist on line 73. Also, if a monster is killed and the corpse owner is now nil for whatever reason, shouldn't the creature still drop loot? In your example, you return early if player is nil or no stamina, so no loot is generated? Tbh, I think the stamina/loot behaviour all depends on what version the OP is using, so it should be up to them on the desired effect.

I guess my example also needs to be slightly fixed as it would drop loot even with no stamina.
(Although... in my opinion, no OT server should be using stamina whatsoever, as it limits gameplay...)
 
Last edited:
@Mateus Robeerto There are still a few flaws in your script, such as player may not exist on line 73. Also, if a monster is killed and the corpse owner is now nil for whatever reason, shouldn't the creature still drop loot? In your example, you return early if player is nil or no stamina, so no loot is generated?
I just realized, but his script is quite confusing. I'm completely giving up on rewriting it; I won't waste another attempt to correct it, haha.
 
I just realized, but his script is quite confusing. I'm completely giving up on rewriting it; I won't waste another attempt to correct it, haha.
Agreed. I'm not too sure either xD

@OTcreator can you clarify in detail exactly what you want because you haven't really made it clear from the start...
 
Back
Top