• 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 Item - daily reward

PuszekLDZ

https://tibia74.eu
Joined
Jan 2, 2020
Messages
428
Solutions
2
Reaction score
109
Location
Lodz
Twitch
puszekgamer
Hello all!

I trying to make a script, that You can use Item as a daily reward..

I merged some, and script works in 80% because I can use item, it gives me gold, changing storage - i got msg about that time left, but even then i can get new reward etc...

Can someone see that? I should work like that.

Player use item, gets coins and time is start counting to use it again.

My code is
LUA:
local cooldown_storage = 88349
local presents = {
    [3218] = { -- item id of the item that will give u random items
        {3031, 10}, {3031, 20}, {3031, 50}, {3031, 100}--Setup the rewards here
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local count = 1
    local targetItem = presents[item.itemid]
    if not targetItem then
        return true
    end

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        count = gift[2]
        gift = gift[1]
    end
    
    local time_left = player:getStorageValue(cooldown_storage)
    if time_left > os.time() then
        time_left = time_left - os.time() -- get the difference in time so it shows properly in error message
        local hours = string.format("%02.f", math.floor(time_left / 3600))
        local mins = string.format("%02.f", math.floor(time_left / 60 - (hours * 60)))
        local secs = string.format("%02.f", math.floor(time_left  - hours * 3600 - mins * 60))
        local time_string = hours ..":".. mins ..":".. secs
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot enter this teleport yet. Time left: ".. time_string .. ".")
        player:addItem(gift, count)
        return false
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You got ".. (count ~= nil and count .. ' ' or '') .. ItemType(gift):getName())
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    player:setStorageValue(cooldown_storage, os.time() + 30)
    return true
end
 
Fixed cooldown bug: now checks the time before giving rewards, preventing multiple unintended item uses. Moved some code from below to above, keeping it organized.

LUA:
local cooldown_storage = 88349
local presents = {
    [3218] = { -- item id of the item that will give u random items
        {3031, 10}, {3031, 20}, {3031, 50}, {3031, 100} --Setup the rewards here
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local count = 1
    local targetItem = presents[item.itemid]
    if not targetItem then
        return true
    end

    local time_left = player:getStorageValue(cooldown_storage)
    if time_left > os.time() then
        time_left = time_left - os.time()
        local hours = string.format("%02.f", math.floor(time_left / 3600))
        local mins = string.format("%02.f", math.floor(time_left / 60 - (hours * 60)))
        local secs = string.format("%02.f", math.floor(time_left - hours * 3600 - mins * 60))
        local time_string = hours ..":".. mins ..":".. secs
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You need to wait: ".. time_string)
        return false
    end

    local gift = targetItem[math.random(#targetItem)]
    if type(gift) == "table" then
        count = gift[2]
        gift = gift[1]
    end

    player:addItem(gift, count)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You received ".. count .. ' ' .. ItemType(gift):getName())
    fromPosition:sendMagicEffect(CONST_ME_GIFT_WRAPS)
    player:setStorageValue(cooldown_storage, os.time() + 30)
    return true
end
 
why it can't be on ground? i dont see any line that will return something wrong when you use it from ground, give console log please.
LUA:
        if fromPosition.x ~= CONTAINER_POSITION then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You can't use this on the ground.")
        return true
        end
fixed all :)
 

Similar threads

Back
Top