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

Chest reward ( random item )

Nuelman

Member
Joined
Nov 9, 2017
Messages
98
Solutions
1
Reaction score
6
V 10.99 tfs 1.x
Hello,
Already I have this script:
Lua:
local rewards = {
  { item = 9019, count = 1 },
  { item = 5809, count = 1 },
  { item = 8982, count = 1 }
}   

local YOUR_STORAGE_NUMBER = 5000

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if player:getStorageValue(YOUR_STORAGE_NUMBER) ~= 1 then
        local random = math.random(1, #rewards)
        player:addItem(rewards[random].item, rewards[random].count)
        player:sendTextMessage(36, 'You have found '.. ItemType(rewards[random].item):getName() ..'.')
        player:setStorageValue(YOUR_STORAGE_NUMBER, 1)
    else
         player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You already opened this chest.")
    end
    return true
end

I would like to change the possibility of obtaining for each object. (chance)

I would also like to add, that you can use the reward chest again after 10 hours.

Is it possible?

+rep
 
Solution
But is diferent versión tfs.
THIS work for 10.99m

And On the question, to use the chest again after a few hours, is there a solution? Thank you
Post automatically merged:

Up
This should work.

-- minor edit to code to fix a small issue on line 27
Lua:
local rewards = {
    { item = 9019, count = 1, chance = 50 },
    { item = 5809, count = 1, chance = 50 },
    { item = 8982, count = 1, chance = 50 }
}

local YOUR_STORAGE_NUMBER = 5000
local delay_timer = 36000 -- in seconds 60*60*10 = 10 hours ()

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)

    local cur_time, cur_storage = os.time(), player:getStorageValue(YOUR_STORAGE_NUMBER)
 
    if cur_storage > cur_time then...
Lua:
local rewards = {
    {id = xxxx, chance = yyyy, count = 1},
    {id = xxxx, chance = yyyy, count = 1},
    {id = xxxx, chance = yyyy, count = 1},
    {id = xxxx, chance = yyyy, count = 1},
}

function onUse(cid, item, fromPosition, target, toPosition)
    local player = Player(cid)
    local bag = player:addItem(2000)
    local msg = {"You have recieved a bag containing: "}
    for i = 1, #rewards do
        local reward = rewards[i]
        local rand = math.random(100)
        if rand >= reward.chance then
            local item = bag:addItem(reward.id, reward.count)
            msg[#msg+1] = string.format("%s%s", (count == 1) and item:getArticle() or count, (reward.count == 1) and item:getName() or item:getPluralName())
        end
    end
    msg[#msg+1] = (#msg == 1 and "nothing." or ".")
    player:sendTextMessage(MESSAGE_INFO_DESCR, table.concat(msg))
    return true
end

Credits: Infernum in this post
 
But is diferent versión tfs.
THIS work for 10.99m

And On the question, to use the chest again after a few hours, is there a solution? Thank you
Post automatically merged:

Up
 
Last edited:
But is diferent versión tfs.
THIS work for 10.99m

And On the question, to use the chest again after a few hours, is there a solution? Thank you
Post automatically merged:

Up
This should work.

-- minor edit to code to fix a small issue on line 27
Lua:
local rewards = {
    { item = 9019, count = 1, chance = 50 },
    { item = 5809, count = 1, chance = 50 },
    { item = 8982, count = 1, chance = 50 }
}

local YOUR_STORAGE_NUMBER = 5000
local delay_timer = 36000 -- in seconds 60*60*10 = 10 hours ()

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)

    local cur_time, cur_storage = os.time(), player:getStorageValue(YOUR_STORAGE_NUMBER)
 
    if cur_storage > cur_time then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. os.date("!%Hh %Mm %Ss", cur_storage - cur_time) .. ".")
        return true
    end
 
    local text = "You have found "
    for i = 1, #rewards do
        local rand = math.random(100)
        if rand <= rewards[i].chance then
            player:addItem(rewards[i].item, rewards[i].count)
            if text ~= "You have found " then
                text = text .. ", "
            end
            text = text .. rewards[i].count .. " " .. ItemType(rewards[i].item):getName()
        end         
    end
    if text == "You have found " then
        text = text .. "nothing"
    end
    player:sendTextMessage(36, text .. ".")
    cur_time = cur_time + delay_timer
    player:setStorageValue(YOUR_STORAGE_NUMBER, cur_time)
 
    return true
end
 
Last edited:
Solution
Not work :S

Lua:
Lua Script Error: [Action Interface]
data/actions/scripts/trenbruja1.lua:onUse
data/actions/scripts/trenbruja1.lua:27: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/trenbruja1.lua:27: in function <data/actions/scripts/trenbruja1.lua:10>

what happen??
thanks for u time
 
Not work :S

Lua:
Lua Script Error: [Action Interface]
data/actions/scripts/trenbruja1.lua:onUse
data/actions/scripts/trenbruja1.lua:27: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/trenbruja1.lua:27: in function <data/actions/scripts/trenbruja1.lua:10>

what happen??
thanks for u time

Change "random" to "rand" in line #27
 
Not work :S
Code:
Lua Script Error: [Action Interface]
data/actions/scripts/trenbruja2.lua:onUse
data/actions/scripts/trenbruja2.lua:27: attempt to index a nil value
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/trenbruja2.lua:27: in function <data/actions/scripts/trenbruja2.lua:10>
Random is random but he lets me collect my reward all the time and I get that mistake in the client...

This work random :
Code:
local rewards = {
  { item = 9019, count = 1 },
  { item = 5809, count = 1 },
  { item = 8982, count = 1 }
}   

local YOUR_STORAGE_NUMBER = 5000

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
    if player:getStorageValue(YOUR_STORAGE_NUMBER) ~= 1 then
        local random = math.random(1, #rewards)
        player:addItem(rewards[random].item, rewards[random].count)
        player:sendTextMessage(36, 'You have found '.. ItemType(rewards[random].item):getName() ..'.')
        player:setStorageValue(YOUR_STORAGE_NUMBER, 1)
    else
         player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You already opened this chest.")
    end
    return true
end
The only thing I need to add to that script is to allow the reward to be picked up again after a few hours.

thanks
 
This is Xikini's script with the line I told you to change fixed.
Lua:
local rewards = {
    { item = 9019, count = 1, chance = 50 },
    { item = 5809, count = 1, chance = 50 },
    { item = 8982, count = 1, chance = 50 }
}

local YOUR_STORAGE_NUMBER = 5000
local delay_timer = 36000 -- in seconds 60*60*10 = 10 hours ()

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)

    local cur_time, cur_storage = os.time(), player:getStorageValue(YOUR_STORAGE_NUMBER)

    if cur_storage > cur_time then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. os.date("!%Hh %Mm %Ss", cur_storage - cur_time) .. ".")
        return true
    end

    local text = "You have found "
    for i = 1, #rewards do
        local rand = math.random(100)
        if rand <= rewards[i].chance then
            player:addItem(rewards[i].item, rewards[i].count)
            if text ~= "You have found " then
                text = text .. ", "
            end
            text = text .. rewards[i].count .. " " .. ItemType(rewards[i].item):getName()
        end        
    end
    if text == "You have found " then
        text = text .. "nothing"
    end
    player:sendTextMessage(36, text .. ".")
    cur_time = cur_time + delay_timer
    player:setStorageValue(YOUR_STORAGE_NUMBER, cur_time)

    return true
end
 
Last edited:
This is Xikini's script with the line I told you to change fixed.
Lua:
local rewards = {
    { item = 9019, count = 1, chance = 50 },
    { item = 5809, count = 1, chance = 50 },
    { item = 8982, count = 1, chance = 50 }
}

local YOUR_STORAGE_NUMBER = 5000
local delay_timer = 36000 -- in seconds 60*60*10 = 10 hours ()

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)

    local cur_time, cur_storage = os.time(), player:getStorageValue(YOUR_STORAGE_NUMBER)

    if cur_storage > cur_time then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. os.date("!%Hh %Mm %Ss", cur_storage - cur_time) .. ".")
        return true
    end

    local text = "You have found "
    for i = 1, #rewards do
        local rand = math.random(100)
        if rand <= rewards[i].chance then
            player:addItem(rewards[i].item, rewards[i].count)
            if text ~= "You have found " then
                text = text .. ", "
            end
            text = text .. rewards[i].count .. " " .. ItemType(rewards[rand].item):getName()
        end        
    end
    if text == "You have found " then
        text = text .. "nothing"
    end
    player:sendTextMessage(36, text .. ".")
    cur_time = cur_time + delay_timer
    player:setStorageValue(YOUR_STORAGE_NUMBER, cur_time)

    return true
end
it should actually be
Lua:
text = text .. rewards[i].count .. " " .. ItemType(rewards[rand].item):getName()
text = text .. rewards[i].count .. " " .. ItemType(rewards[i].item):getName() -- this
Thanks for helping though. <3
 
its works, now givie just 1 item, but sometimes givie you nothing, and i am looking to always give you 1 item at least, no probability, just give you 1 random item of the 3
 
Last edited:
its works, now givie just 1 item, but sometimes givie you nothing, and i am looking to always give you 1 item at least, no probability, just give you 1 random item of the 3
Lua:
local rewards = {
    { item = 9019, count = 1 },
    { item = 5809, count = 1 },
    { item = 8982, count = 1 }
}

local YOUR_STORAGE_NUMBER = 5000
local delay_timer = 36000 -- in seconds 60*60*10 = 10 hours ()

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
  
    local cur_time, cur_storage = os.time(), player:getStorageValue(YOUR_STORAGE_NUMBER)
  
    if cur_storage > cur_time then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. os.date("!%Hh %Mm %Ss", cur_storage - cur_time) .. ".")
        return true
    end
  
    local rand = math.random(#rewards)
    player:addItem(rewards[rand].item, rewards[rand].count)
    player:sendTextMessage(36, "You have found " .. rewards[rand].count .. " " .. ItemType(rewards[rand].item):getName() .. ".")

    cur_time = cur_time + delay_timer
    player:setStorageValue(YOUR_STORAGE_NUMBER, cur_time)
  
    return true
end
 
Last edited:
Thank you Xikini :D!
Post automatically merged:

How can put the days remain, if the time remains cross the 24hrs?

change
Lua:
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. os.date("!%Hh %Mm %Ss", cur_storage - cur_time) .. ".")
to
Lua:
local timer = cur_storage - cur_time
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can obtain this reward again in " .. (timer > 86399 and os.date("!%dd %Hh", timer) or os.date("!%Hh %Mm %Ss", timer)) .. ".")

If greater then 24h will display
Code:
You can obtain this reward again in 02d 02h.
otherwise will show
Code:
You can obtain this reward again in 23h 59m 58s.
 
Last edited:
Back
Top