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

Random item from chest.

Lopaskurwa

Active Member
Joined
Oct 6, 2017
Messages
873
Solutions
2
Reaction score
49
How to add random items into a this script
Lua:
local storage_id = 2544

TFS 1.2
local rewards = {
    [1] = {chest_uid = 1575, reward_id = 16857, reward_count = 25}
}

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, #rewards do
        if rewards[i].chest_uid == item.uid then
            reward = rewards[i]
            break
        end
    end

    local reward_type = ItemType(reward.reward_id)
    if reward_type then
        if player:addItem(reward.reward_id, reward.reward_count, false, 1, CONST_SLOT_WHEREEVER) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a " .. reward_type:getName():lower() .. ".")
            player:setStorageValue(storage_id, 1)
        else
            local weight = reward_type:getWeight()
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found an item weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
        end
    end
    return true
end
reward_id = 16857 and count should be not random. But i want to add more items into this one chest and those items are random so, 16857 is not random and 100,101,102 is random, so you get 16857 and random item from (100,101,102) i though about combining with this script Random reward Chest (https://otland.net/threads/random-reward-chest.253684/) but then every item will be random so i have no idea
 
Solution
How to add random items into a this script
Lua:
local storage_id = 2544

TFS 1.2
local rewards = {
    [1] = {chest_uid = 1575, reward_id = 16857, reward_count = 25}
}

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, #rewards do
        if rewards[i].chest_uid == item.uid then
            reward = rewards[i]
            break
        end
    end

    local reward_type = ItemType(reward.reward_id)
    if reward_type then
        if player:addItem(reward.reward_id, reward.reward_count, false, 1, CONST_SLOT_WHEREEVER)...
How to add random items into a this script
Lua:
local storage_id = 2544

TFS 1.2
local rewards = {
    [1] = {chest_uid = 1575, reward_id = 16857, reward_count = 25}
}

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, #rewards do
        if rewards[i].chest_uid == item.uid then
            reward = rewards[i]
            break
        end
    end

    local reward_type = ItemType(reward.reward_id)
    if reward_type then
        if player:addItem(reward.reward_id, reward.reward_count, false, 1, CONST_SLOT_WHEREEVER) then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You have found a " .. reward_type:getName():lower() .. ".")
            player:setStorageValue(storage_id, 1)
        else
            local weight = reward_type:getWeight()
            player:sendTextMessage(MESSAGE_INFO_DESCR, 'You have found an item weighing ' .. weight / 100 .. ' oz it\'s too heavy or you do not have enough room.')
        end
    end
    return true
end
reward_id = 16857 and count should be not random. But i want to add more items into this one chest and those items are random so, 16857 is not random and 100,101,102 is random, so you get 16857 and random item from (100,101,102) i though about combining with this script Random reward Chest (https://otland.net/threads/random-reward-chest.253684/) but then every item will be random so i have no idea
I hope I understood what you needed. Try this, it's untested.
Lua:
local storage_id = 2544

local static_rewards = {
    [1] = {chest_uid = 1575, reward_id = 16857, reward_count = 25}
}

local random_rewards = {
    [1] = {chest_uid = 1575, reward_id = 100, reward_count = 1},
    [2] = {chest_uid = 1575, reward_id = 101, reward_count = 1},
    [3] = {chest_uid = 1575, reward_id = 102, 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, #static_rewards do
        if static_rewards[i].chest_uid == item.uid then
            reward = static_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
 
Solution
I hope I understood what you needed. Try this, it's untested.
Lua:
local storage_id = 2544

local static_rewards = {
    [1] = {chest_uid = 1575, reward_id = 16857, reward_count = 25}
}

local random_rewards = {
    [1] = {chest_uid = 1575, reward_id = 100, reward_count = 1},
    [2] = {chest_uid = 1575, reward_id = 101, reward_count = 1},
    [3] = {chest_uid = 1575, reward_id = 102, 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, #static_rewards do
        if static_rewards[i].chest_uid == item.uid then
            reward = static_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
Yep you understood me right thats what i needed.
 
Back
Top