• 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!

Action [TFS 1.X] Reward Chest

@Elwyn

REWARD AND REWARDS ARE TWO DIFFERENT THINGS LOL

LMAO! :D :D :D xD. thanks for the script this saves me allot of scripts :p BUT

If you use the same uniqueid for two different chests, you'll get a warning when starting up the server and the player will only be able to open one of the two chests. < THIS doesnt work for me

my tibia strangely only makes 1 chest with that unique id while in my remeres i made 2 chests with the same unique id. i made a video can u help me? :p

i need it so that i can make a set for each vocation instead that the player takes all items/chests :p

enlarge for better view + set quality better ofc.

 
Last edited:
@Elwyn



LMAO! :D :D :D xD. thanks for the script this saves me allot of scripts :p BUT

If you use the same uniqueid for two different chests, you'll get a warning when starting up the server and the player will only be able to open one of the two chests. < THIS doesnt work for me

my tibia strangely only makes 1 chest with that unique id while in my remeres i made 2 chests with the same unique id. i made a video can u help me? :p

i need it so that i can make a set for each vocation instead that the player takes all items/chests :p

enlarge for better view + set quality better ofc.
You can only have one of each unique id. If you want to be able to give different items for different vocations, you either need a different chest for each and maybe limit access to the rooms for each vocation, or you need a modified script to check which vocation is opening the chest and yield different items accordingly.
 
It works wrong for plural items.
For example: I set a chest with 15 platinum coins and I only get 1 platinum coin when I open it.
 
In
Code:
        player:addItem(reward:getId(), 1)

Change to:
Code:
        player:addItem(reward:getId(), reward:getCount())
 
Any idea how to make keys with action id or books with description inside?
 
Change
Code:
player:addItem(reward:getId(), reward:getCount())
to
Code:
player:addItem(reward:getId(), reward:getCount()):setActionId(reward:getActionId())
 
Change
Code:
player:addItem(reward:getId(), reward:getCount())
to
Code:
player:addItem(reward:getId(), reward:getCount()):setActionId(reward:getActionId())

Thanks for trying but still not getting .. i've set up the action id for the key but when getting from chest the key given have no action id attached..

hkflSZe.png

Code:
13:43 You see a wooden key (Key:0).
It weighs 1.00 oz.
Item ID: 2087
Position: 394, 95, 10
 
Well it works for me, I actually replaced it with
Code:
if reward:getActionId() > 1000 then
    player:addItem(reward:getId(), reward:getCount()):setActionId(reward:getActionId())
else
    player:addItem(reward:getId(), reward:getCount())
end
Because if it doesnt have an actionid it would set it to 100 instead of none.
I use the latest nigthly release, dunno about you.
 
My entire section

Code:
        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(uniqueid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                if reward:getActionId() > 1000 then
                    player:addItem(reward:getId(), reward:getCount()):setActionId(reward:getActionId())
                else
                    player:addItem(reward:getId(), reward:getCount())
                end
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')

            player:setStorageValue(uniqueid, -2)
        end
    end

i tried with your option and still nothing.. im using this one https://github.com/brunominervino/forgottenserver .. is this one the problem?
 
I don't know what to tell you, I downloaded that and compiled it and it works for me. I modified more for mine, I don't use the uniqueid number as the storagevalue key, but the actionid, so that I can have multiple chests with the same storage for quests that you have to choose one. Anyway, here is the entire thing:
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)

    if not chest then
        return true
    end

    local actionid = chest:getActionId()
    if (actionid < 1000) then
        return false
    end

    if player:getStorageValue(actionid) == -2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local reward = nil
    local start = player:getStorageValue(actionid) == -1 and 0 or player:getStorageValue(actionid)

    for i = start, chest:getSize() do
        reward = chest:getItem(i)
        if not reward then
            break
        end

        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(actionid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                if reward:getActionId() > 1000 then
                    player:addItem(reward:getId(), reward:getCount()):setActionId(reward:getActionId())
                else
                    player:addItem(reward:getId(), reward:getCount())
                end
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')

            player:setStorageValue(actionid, -2)
        end
    end

    if actionid == 5500 then
        player:setStorageValue(actionid, 0)
    end

    return true
end

and then you gotta put the itemid of the item(s) you want to use as reward chests on actions.xml, instead of actionid 15000
Code:
<action itemid="1740" script="reward.lua" />

It's working fine here, if it doesn't for you and there are no errors on console I have no idea.
 
@barter thanks for helping..

i just compiled it again and installed everything that i was using and surprisingly the code that you gave before with if and else worked.. weird dunno what happened.. but nothing is lost :p the code from your last comment will be useful :D really nice work

Thanks once again
 
Hey there!

I am creating my first OT server (TFS1), and I have been learning a lot with it. A lot of helpful information has been found on this forum, thanks so much for that! ^^

I found this script and implemented in my server's map exactly as OP's instructions, but the reward chest continues behaving just like any other chest (it spawns the item I have put inside it only once: and if the first character who opens it gets the item, the next characters to open it will find nothing inside. Only after a server restart will the item spawn again inside the chest, and that same first character will be able to get the items again, excluding all others).

Whenever the reward chest is opened, the console returns the following error:
Lua Script Error: [Action Interface]
data/actions/scripts/reward.lua : onUse
data/actions/scripts/reward.lua:9: attempt to index local 'player' (a number value)
stack traceback:
[C]: in function '__index'
data/actions/scripts/reward.lua:9: in function <data/actions/scripts/reward.lua:1>

Inside the "/data/actions/scripts" directory there is: "quests" "tools" "other" "reward.lua"

I saw Jknot's posts above, and I thought it could be a similar situation, but as the images he submitted are no longer available, I cannot be sure of it. and, anyway, I was not able to find answers anywhere else.

Can anyone please help me with a solution? Is there any other useful info I should bring here (it's my first time XD)

Thank you in advance for your help ;)



It's meant to help mappers who doesn't know Lua, so they can make their quest chest reward without the help of scripters.

Create a reward.lua at actions/scripts and copy-paste the following code:
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local chest = Container(item.uid)

    if not chest then
        return true
    end

    local uniqueid = chest:getUniqueId()
    if player:getStorageValue(uniqueid) == -2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
        return true
    end

    local reward = nil
    local start = player:getStorageValue(uniqueid) == -1 and 0 or player:getStorageValue(uniqueid)

    for i = start, chest:getSize() do
        reward = chest:getItem(i)
        if not reward then
            break
        end

        if reward:getWeight() > player:getFreeCapacity() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')
            player:setStorageValue(uniqueid, i)
            break
        else
            local reward_container = Container(reward:getUniqueId())
            if reward_container then
                reward_container = reward_container:clone()
                reward_container:moveTo(player)
            else
                player:addItem(reward:getId(), reward:getCount())
            end
            local reward_msg = reward:getArticle() .. ' ' .. reward:getName()
            if reward:getCount() > 1 then
                reward_msg = reward:getCount() .. ' ' .. reward:getPluralName()
            end

            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found ' .. reward_msg .. '.')

            player:setStorageValue(uniqueid, -2)
        end
    end

    return true
end
Then register at actions.xml using the following tag:

Code:
  <action actionid="15000" script="reward.lua"/>

To use it, when mapping your quest, create a chest and change its actionid to 15000 and give a uniqueid to it. Remember to not use a uniqueid that's already in use, and only use uniqueid whose the storage of the same value is not being used.

If you use the same uniqueid for two different chests, you'll get a warning when starting up the server and the player will only be able to open one of the two chests.
 
Last edited:
I made some adjusts on the script, now it should work for you @raganius
With the adjust, it will work for any TFS 1.x, as long as future versions adhere to backwards compatibility.
Now items with modified attributes inside the chest, such as actionid or subtype, will be properly sent to the player with the same attributes.
 
Thanks, @Elwyn , for the fast reply ^^

I tested the new code and what happens is, the reward chest will open to the first character, and the rewarded items will be there. But after that, it won't open again untill a server restart.

Restarting the server, the chest will open again to the first character (even if it's the same character who received the rewarded items before).

The error I get now it:

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/reward.lua: onUse
data/actions/scripts/reward.lua:21: attempt to call method 'getWeight' (a nil value)
stack traceback:
        [C]: in function 'getWeight'
        data/actions/scripts/reward.lua:21: in function <data/actions/scripts/reward.lua:1>
 
Thanks, @Elwyn , for the fast reply ^^

I tested the new code and what happens is, the reward chest will open to the first character, and the rewarded items will be there. But after that, it won't open again untill a server restart.

Restarting the server, the chest will open again to the first character (even if it's the same character who received the rewarded items before).

The error I get now it:

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/reward.lua: onUse
data/actions/scripts/reward.lua:21: attempt to call method 'getWeight' (a nil value)
stack traceback:
        [C]: in function 'getWeight'
        data/actions/scripts/reward.lua:21: in function <data/actions/scripts/reward.lua:1>

Change reward:getWeight() to ItemType(reward:getId()):getWeight(reward:getCount()), I'll change it in the main post
 
Thank you again, @Elwyn I have made the change, but with no luck :/ as the error is now

[quote
Lua Script Error: [Action Interface]
data/actions/scripts/reward.lua: onUse
data/actions/scripts/reward.lua:29: attempt to call method 'getWeight' (a nil value)
stack traceback:
[C]: in function 'getWeight'
data/actions/scripts/reward.lua:29: in function <data/actions/scripts/reward.lua:1>
[/quote]

line 29 is
" player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a ' .. reward:getName() .. ' weighing ' .. reward:getWeight()/100 .. ' oz it\'s too heavy.')"
I have tried commenting it out wit a "-- ", but it did now work, anyway.

I'll keep watching, and looking for a solution ;)
 
Back
Top