• 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.2] Simple: Use Item Receive Gift

Jeffro

Alpha
Joined
Jan 17, 2015
Messages
235
Reaction score
262
Location
New York
Someone requested just a simple use item and receive items back.
It is intended for TFS 1.2 and I haven't tested it at all.

./actions.xml
Code:
<action itemid="ITEMID" script="SCRIPT.LUA" />

./actions/scripts/SCRIPT.LUA
Code:
local config = {storage = 1000, canclemsg = "You've already used this item!",
  reward = {1992, 1993}
}

function onUse(player, item)
    if player:isPlayer and player:getStorageValue(config.storage) <= 0  then
        for gift = 1, #config.reward do
            player:addItem(config.reward[gift], 1)
            player:setStorageValue(config.storage, 1)
        end
    else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, config.canclemsg, player:getPosition())
    end
end
--Credit to tetra20 for fixing bugs, but still report an issue
 
Last edited:
Code:
for gift = config.reward, #gift do

should be

Code:
for gift = 1,#config.reward do

# return the number of elements inside an array but it ignores nil..

then add
Code:
player:addItem(config.reward[gift], 1)

add this
Code:
player:setStorageValue(config.storage, 1)

After the for loop, to avoid setting storage multiple times.. storage lags because it is sql queries.. much of them will lag

Also here
Code:
player:getStorageValue(config.storage) == 0

it is prefered to be

Code:
player:getStorageValue(config.storage) <= 0

Sometimes the storage is -1 or nil by default and not 0

Great Job As Always Mate! Keep it up!:D
 
Code:
if player:isPlayer and player:getStorageValue(config.storage) <= 0  then

to

Code:
if player:isPlayer() and player:getStorageValue(config.storage) <= 0  then
 
no more errors but qhen i use it... you cant use this object thanks you


Code:
if player:isPlayer and player:getStorageValue(config.storage) <= 0  then

to

Code:
if player:isPlayer() and player:getStorageValue(config.storage) <= 0  then
 
There are so many problems with that code...I wouldn't credit anyone because its not worth anything....

With this code you can use one script to handle all chests... Just set the UID in the map editor of the chest to the same as in the code...

For example in the code you see 4000 that is the UID for the chest.

Lua:
local chests = {
[4000] = {items = {[1] = {itemid = 1992, count = 1}, [2] = {itemid = 1993, count = 1}}}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    CHEST = chests[item:getAttribute('uid')]
   
    if CHEST then
        if player:getStorageValue(item:getAttribute('uid')) == -1  then
            for i = 1, #CHEST.items do
                player:addItem(CHEST.items.itemid, CHEST.items.count)
            end
            player:setStorageValue(item:getAttribute('uid'), 1)
        else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "It is empty.")
    end
    end
    return true
end
 
and you can do this:

Code:
local chests = {
[4000] = {items = {[1] = {itemid = 1992, count = 1}, [2] = {itemid = 1993, count = 1}}},
[4001] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}},
[4002] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}}
}

It also lets you choose how many of the item to give them.
 
Shit..made a mistake lmao here is the code...

Lua:
local chests = {
[4000] = {items = {[1] = {itemid = 1992, count = 1}, [2] = {itemid = 1993, count = 1}}},
[4001] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}},
[4002] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    CHEST = chests[item:getAttribute('uid')]
   
    if CHEST then
        if player:getStorageValue(item:getAttribute('uid')) == -1  then
            for i = 1, #CHEST.items do
                player:addItem(CHEST.items[i].itemid, CHEST.items[i].count)
            end
            player:setStorageValue(item:getAttribute('uid'), 1)
        else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "It is empty.")
    end
    end
    return true
end
 
bro not is a chest xD is one item when i clicking it get some items x3 but thanks you

Shit..made a mistake lmao here is the code...

Lua:
local chests = {
[4000] = {items = {[1] = {itemid = 1992, count = 1}, [2] = {itemid = 1993, count = 1}}},
[4001] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}},
[4002] = {items = {[1] = {itemid = 111, count = 1}, [2] = {itemid = 111, count = 1}}}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    CHEST = chests[item:getAttribute('uid')]
  
    if CHEST then
        if player:getStorageValue(item:getAttribute('uid')) == -1  then
            for i = 1, #CHEST.items do
                player:addItem(CHEST.items[i].itemid, CHEST.items[i].count)
            end
            player:setStorageValue(item:getAttribute('uid'), 1)
        else
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "It is empty.")
    end
    end
    return true
end
 
oh.... Here's that then.

Lua:
local storage = 10000
local items = {
[1] = {itemid = 1992, count = 1},
[2] = {itemid = 1993, count = 1},
}

function onUse(player, item, fromPosition, target, toPosition)
     if player:getStorageValue(storage) ~= -1 then
          return player:sendCancelMessage("You cannot use this item more than once.")
     end

for i = 1, #items do
     player:addItem(items[i].itemid, items[i].count)
end
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have received your items!")
player:setStorageValue(storage, 1)
return true
end
 
thanks you so much

oh.... Here's that then.

Lua:
local storage = 10000
local items = {
[1] = {itemid = 1992, count = 1},
[2] = {itemid = 1993, count = 1},
}

function onUse(player, item, fromPosition, target, toPosition)
     if player:getStorageValue(storage) ~= -1 then
          return player:sendCancelMessage("You cannot use this item more than once.")
     end

for i = 1, #items do
     player:addItem(items[i].itemid, items[i].count)
end
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have received your items!")
player:setStorageValue(storage, 1)
return true
end
 
How do I get 1 item from a random list, with a percentage?

list

local item1 = 5894,10 -- 30 % de chance
local item2 = 5896,10 -- 60 % de chance
local item3 = 5891 -- 10% de chance
 
-- Time Chest by Limos
local config = {
exhausttime = 24*60*60, -- time
exhauststorage = 2300,
level = 100 -- minimum level to open the chest
}

local rewarditems = {
[100] = {
nextlevel = 200,
{id = 3982, chance = 5, count = 1}, -- start with the lowest chances
{id = 2476, chance = 10, count = 1},
{id = 2479, chance = 15, count = 1},
{id = 2148, chance = 70, count = math.random(1, 50)}
},
[200] = {
nextlevel = 300,
{id = 7730, chance = 5, count = 1},
{id = 2466, chance = 10, count = 1},
{id = 2497, chance = 15, count = 1},
{id = 2152, chance = 70, count = math.random(1, 20)}
},
[300] = {
nextlevel = 400,
{id = 2492, chance = 5, count = 1},
{id = 2498, chance = 10, count = 1},
{id = 2195, chance = 15, count = 1},
{id = 2152, chance = 70, count = math.random(20, 50)}
},
[400] = {
nextlevel = 500,
{id = 2472, chance = 5, count = 1},
{id = 2470, chance = 10, count = 1},
{id = 2157, chance = 15, count = 1},
{id = 2160, chance = 70, count = math.random(1, 5)}
}
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
if player:getLevel() < config.level then
doSendMagicEffect(pos, CONST_ME_POFF)
player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need to be level "..config.level.." to open the box.")
return true
end

if player:getStorageValue(config.exhauststorage) > os.time() then
local time = player:getStorageValue(config.exhauststorage) - os.time()
local hours, minutes, seconds = math.floor (time / 3600), math.floor ((time - ((math.floor (time / 3600)) * 3600))/ 60), time - ((math.floor (time/60)) * 60)
if time >= 3600 then
text = hours.." "..(hours == 1 and "hour" or "hours")..", "..minutes.." "..(minutes == 1 and "minute" or "minutes").." and "..seconds.." "..(seconds == 1 and "second" or "seconds")
elseif time >= 60 then
text = minutes.." "..(minutes == 1 and "minute" or "minutes").." and "..seconds.." "..(seconds == 1 and "second" or "seconds")
else
text = seconds.." "..(seconds == 1 and "second" or "seconds")
end
doSendMagicEffect(pos, CONST_ME_POFF)
player:sendTextMessage(MESSAGE_INFO_DESCR, "You need to wait "..text.." before you can get a reward again.")
return true
end

local chance = math.random(1,100)
for v, x in pairs(rewarditems) do
if player:getLevel() >= v and player:getLevel() < x.nextlevel then
level = v
end
end

for i = 1, #rewarditems[level], 1 do
if chance < rewarditems[level].chance then
local itemType = ItemType(rewarditems[level].id)
if rewarditems[level].count > 1 then
text = rewarditems[level].count .. " " .. itemType:getPluralName()
else
text = itemType:getArticle() .. " " .. itemType:getName()
end

local reward = player:addItem(rewarditems[level].id, rewarditems[level].count)
text = "You have found " .. text .. "."
player:addItem(reward)
player:setStorageValue(config.exhauststorage, os.time() + 24*60*60)
player:sendTextMessage(MESSAGE_INFO_DESCR, text)
return true
else
chance = chance - rewarditems[level].chance
end
end
end
 
Back
Top