• 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 box error

henkas

Well-Known Member
Joined
Jul 8, 2015
Messages
993
Solutions
5
Reaction score
55
35540
Lua:
local storage_id = 2545

local random_rewards = {
    [1] = {chest_uid = 1577, reward_id = 14282, reward_count = 1},
    [2] = {chest_uid = 1577, reward_id = 14361, reward_count = 1},
    [3] = {chest_uid = 1577, reward_id = 14358, reward_count = 1},
    [4] = {chest_uid = 1577, reward_id = 14360, reward_count = 1},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storage = player:getStorageValue(storage_id)
    if storage > 0 then
        return player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end

    local randoms = {}
    for i = 1, #random_rewards do
        if random_rewards[i].chest_uid == item.uid then
            randoms[#randoms + 1] = random_rewards[i]
        end
    end

    local bag = Game.createItem(1987)
    bag:addItem(reward.reward_id, reward.reward_count)
    local random_num = math.random(1, #randoms)
    bag:addItem(randoms[random_num].reward_id, randoms[random_num].reward_count)

    if player:addItemEx(bag) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a bag.")
        player:setStorageValue(storage_id, 1)
    else
        local weight = bag:getWeight()
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a bag weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
    end
    return true
end
TFS 1.2
 
Solution
Lets start with this array
Lua:
local random_rewards = {
    [1] = {chest_uid = 1577, reward_id = 14282, reward_count = 1},
    [2] = {chest_uid = 1577, reward_id = 14361, reward_count = 1},
    [3] = {chest_uid = 1577, reward_id = 14358, reward_count = 1},
    [4] = {chest_uid = 1577, reward_id = 14360, reward_count = 1},
}
One chest contains 4 different rewards.

Now our reward variable
Lua:
local reward
for i = 1, #random_rewards do
    if random_rewards[i].chest_uid == item.uid then
        reward = random_rewards[i]
        break
    end
end
Based on that, first value from random_rewards will be assigned to that reward variable, that's because of break.

Then there are randoms
Lua:
local...
Post entire script after changes.
Lua:
local storage_id = 2545

local random_rewards = {
    [1] = {chest_uid = 1577, reward_id = 14282, reward_count = 1},
    [2] = {chest_uid = 1577, reward_id = 14361, reward_count = 1},
    [3] = {chest_uid = 1577, reward_id = 14358, reward_count = 1},
    [4] = {chest_uid = 1577, reward_id = 14360, reward_count = 1},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storage = player:getStorageValue(storage_id)
    if storage > 0 then
        return player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end

    local reward
    for i = 1, #random_rewards do
    if random_rewards[i].chest_uid == item.uid then
    reward = random_rewards[i]
    break
    end
    end

    local randoms = {}
    for i = 1, #random_rewards do
        if random_rewards[i].chest_uid == item.uid then
            randoms[#randoms + 1] = random_rewards[i]
        end
    end

    local bag = Game.createItem(1987)
    bag:addItem(reward.reward_id, reward.reward_count)
    local random_num = math.random(1, #randoms)
    bag:addItem(randoms[random_num].reward_id, randoms[random_num].reward_count)

    if player:addItemEx(bag) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a bag.")
        player:setStorageValue(storage_id, 1)
    else
        local weight = bag:getWeight()
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a bag weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
    end
    return true
end
So i tried recreating whole local function then i got different error, if i use two local i get two items, so i cant figure it out they correct way of it
 
Lets start with this array
Lua:
local random_rewards = {
    [1] = {chest_uid = 1577, reward_id = 14282, reward_count = 1},
    [2] = {chest_uid = 1577, reward_id = 14361, reward_count = 1},
    [3] = {chest_uid = 1577, reward_id = 14358, reward_count = 1},
    [4] = {chest_uid = 1577, reward_id = 14360, reward_count = 1},
}
One chest contains 4 different rewards.

Now our reward variable
Lua:
local reward
for i = 1, #random_rewards do
    if random_rewards[i].chest_uid == item.uid then
        reward = random_rewards[i]
        break
    end
end
Based on that, first value from random_rewards will be assigned to that reward variable, that's because of break.

Then there are randoms
Lua:
local randoms = {}
for i = 1, #random_rewards do
    if random_rewards[i].chest_uid == item.uid then
        randoms[#randoms + 1] = random_rewards[i]
    end
end
Right now randoms will be the same as random_rewards but that will change if you add more rewards with different chest_uid.

Now bag and items we put there
Lua:
local bag = Game.createItem(1987)
bag:addItem(reward.reward_id, reward.reward_count)
This will add 1st item which is the first one from random_rewards.

Lua:
local random_num = math.random(1, #randoms)
bag:addItem(randoms[random_num].reward_id, randoms[random_num].reward_count)
Now this will add 2nd item but this time it's going to be random.

So you have 2 options. Get rid of reward usage or randoms, whichever you prefer.

Or you can make some chests to give random reward and some to give static reward.
Final script with some cleanup and changes would be like this
Lua:
local storage_id = 2545

local chests = {
    [1577] = {
        random = true,
        rewards = {
            { id = 14282, count = 1},
            { id = 14361, count = 1},
            { id = 14358, count = 1},
            { id = 14360, count = 1}
        }
    },
    [1578] = {
        random = false,
        reward = { id = 14282, count = 1 }
    }
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storage = player:getStorageValue(storage_id)
    if storage > 0 then
        return player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end

    local chest = chests[item.uid]
    if chest then
        local bag = Game.createItem(1987)
        if chest.random == true then
            local random_num = math.random(1, #chest.rewards)
            bag:addItem(chest.rewards[random_num].id, chest.rewards[random_num].count)
        else
            bag:addItem(chest.reward.id, chest.reward.count)
        end

        if player:addItemEx(bag) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a bag.")
            player:setStorageValue(storage_id, 1)
        else
            local weight = bag:getWeight()
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a bag weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
        end
    end
    return true
end

Let me know if you don't understand something and I'll try to explain.
 
Solution
Edit nvm messed up with local. Everything works, thanks for explain it.
Lua:
local storage_id = 2545

local random_rewards = {
    [1] = {chest_uid = 1577, reward_id = 14282, reward_count = 1},
    [2] = {chest_uid = 1577, reward_id = 14361, reward_count = 1},
    [3] = {chest_uid = 1577, reward_id = 14358, reward_count = 1},
    [4] = {chest_uid = 1577, reward_id = 14360, reward_count = 1},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local storage = player:getStorageValue(storage_id)
    if storage > 0 then
        return player:sendTextMessage(MESSAGE_INFO_DESCR, "It is empty.")
    end

     local randoms = {}
    for i = 1, #random_rewards do
    if random_rewards[i].chest_uid == item.uid then
        randoms[#randoms + 1] = random_rewards[i]
    end
    end

    local bag = Game.createItem(1987)
    local random_num = math.random(1, #randoms)
    bag:addItem(randoms[random_num].reward_id, randoms[random_num].reward_count)

    if player:addItemEx(bag) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a bag.")
        player:setStorageValue(storage_id, 1)
    else
        local weight = bag:getWeight()
        player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found a bag weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
    end
    return true
end
 
Last edited:
Back
Top