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

Reward chest

Xarah

Member
Joined
Apr 18, 2018
Messages
39
Reaction score
7
Hello, I have a problem with the daily boss chest. When I try to make the rewards appear in a bag, it suddenly stops assigning any rewards at all. This chest is supposed to give random rewards.

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    -- KONFIGURACJA
    local CHEST_UID = 5802
    local CHEST_COOLDOWN = 86400       -- 24h w sekundach
    local CHEST_STORAGE = 40001        -- Storage do zapisu czasu ostatniego użycia
    local BAG_ID = 1987               -- ID torby (sprawdź czy 1987 to faktycznie bag w Twoim items.xml)

    -- Pula nagród z procentową szansą (w "promilach", np. 2000 = 20%)
    local possibleRewards = {
        {itemId = 2169, minCount = 1, maxCount = 1, name = "Ring of Healing",  chance = 5000},   -- 5%
        {itemId = 2148, minCount = 50, maxCount = 100, name = "Gold Coins",    chance = 5000},  -- 40%
        {itemId = 2152, minCount = 2,  maxCount = 5,  name = "Platinum Coins", chance = 5000},  -- 20%
        {itemId = 7620, minCount = 1,  maxCount = 3,  name = "Mana Potions",   chance = 5000},  -- 20%
        {itemId = 7588, minCount = 1,  maxCount = 2,  name = "Strong Health Potions", chance = 5000} -- 15%
    }

    -- Funkcja do wylosowania JEDNEGO przedmiotu z puli (wg pola 'chance')
    local function getRandomReward()
        local totalChance = 0
        for _, reward in ipairs(possibleRewards) do
            totalChance = totalChance + reward.chance
        end

        local roll = math.random(1, totalChance)
        local runningSum = 0

        for _, reward in ipairs(possibleRewards) do
            runningSum = runningSum + reward.chance
            if roll <= runningSum then
                local count = math.random(reward.minCount, reward.maxCount)
                return reward.itemId, count, reward.name
            end
        end
        -- Zapasowo, gdyby nic się nie wylosowało (teoretycznie niemożliwe):
        return 2148, 1, "Gold Coin"
    end

    -- Sprawdzamy, czy to właściwy UID skrzyni
    if item.uid == CHEST_UID then
        local lastUse = player:getStorageValue(CHEST_STORAGE)
        if lastUse < 1 then
            lastUse = 0
        end

        local timeSinceLast = os.time() - lastUse
        if timeSinceLast < CHEST_COOLDOWN then
            local timeLeft = CHEST_COOLDOWN - timeSinceLast
            player:sendTextMessage(MESSAGE_INFO_DESCR,
                string.format("You have to wait %d seconds to open this chest again.", timeLeft))
            return true
        end

        -- Próba dodania do ekwipunku torby (bag)
        local bag = player:addItem(BAG_ID, 1)
        if not bag then
            player:sendTextMessage(MESSAGE_STATUS_WARNING,
                "You don't have enough capacity to receive the bag.")
            return true
        end

        -- Sprawdzamy, czy jest kontenerem
        if not bag:isContainer() then
            player:sendTextMessage(MESSAGE_STATUS_WARNING,
                "The item ID " .. BAG_ID .. " is not recognized as a container.")
            return true
        end

        local container = bag:getContainer()
        if not container then
            player:sendTextMessage(MESSAGE_STATUS_WARNING,
                "Unexpected error: cannot open bag container in script.")
            return true
        end

        -- Losujemy JEDEN przedmiot i dodajemy go do torby
        local itemId, itemCount, itemName = getRandomReward()
        container:addItem(itemId, itemCount)

        player:sendTextMessage(MESSAGE_EVENT_ADVANCE,
            string.format("You have found a bag containing %dx %s.", itemCount, itemName))

        -- Ustawiamy timestamp w storage, by naliczyć cooldown 24h
        player:setStorageValue(CHEST_STORAGE, os.time())

    else
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "There is nothing here.")
    end

    return true
end


Additionally, an error message appears in the console:
1736370623800.webp
 
Solution
item:getContainer() is "canary" engine function, but it does not exist on TFS.
Container(uid) does the same on TFS.

In your case replace:
LUA:
local container = bag:getContainer()
with:
LUA:
local container = Container(bag.uid)

Anyway, this part of code is not needed at all:
LUA:
        local container = bag:getContainer()
        if not container then
            player:sendTextMessage(MESSAGE_STATUS_WARNING,
                "Unexpected error: cannot open bag container in script.")
            return true
        end
It's already checked above with:
LUA:
if not bag:isContainer() then

EDIT:
This script always adds1 item. If you want to put it in bag, so ring of healing doesn't go into slot...
item:getContainer() is "canary" engine function, but it does not exist on TFS.
Container(uid) does the same on TFS.

In your case replace:
LUA:
local container = bag:getContainer()
with:
LUA:
local container = Container(bag.uid)

Anyway, this part of code is not needed at all:
LUA:
        local container = bag:getContainer()
        if not container then
            player:sendTextMessage(MESSAGE_STATUS_WARNING,
                "Unexpected error: cannot open bag container in script.")
            return true
        end
It's already checked above with:
LUA:
if not bag:isContainer() then

EDIT:
This script always adds1 item. If you want to put it in bag, so ring of healing doesn't go into slot and start decaying, then it's OK.

This comment is a lie:
Code:
-- Pula nagród z procentową szansą (w "promilach", np. 2000 = 20%)
It's not percent or promil chance, it's relative chance between items. If you set all 5 items chance to 1, it won't be 1 item per 200 open boxes (0.1% [1 promil] * 5 = 0.5% = 1 per 200). It will still pick 1 random item from list, all with same 'chance', as all are set to same value '1'.
If you want to make it work like monsters loot. Where each item has own chance and you can get multiple items, if you are lucky, you must rewrite that script.
 
Last edited:
Solution


Write your reply...

Similar threads

V
  • Question Question
Replies
6
Views
360
verdehile95
V
Back
Top