• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

TFS 0.X how to spawn item in chest from pos to pos?

vexler222

Active Member
Joined
Apr 22, 2012
Messages
714
Solutions
15
Reaction score
47
Hi, i have on map 20 chest and i want to spawn items inside if i use switch, but only for 5 chest.
 
Solution
X
[17:54:56.138] [Error - Action Interface]
[17:54:56.138] data/actions/scripts/chestspawn.lua:eek:nUse
[17:54:56.138] Description:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: attempt to index field 'x' (a nil value)
[17:54:56.138] stack traceback:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: in function <data/actions/scripts/chestspawn.lua:119>

[17:55:06.138] [Error - Action Interface]
[17:55:06.138] In a timer event called from:
[17:55:06.138] data/actions/scripts/chestspawn.lua:eek:nUse
[17:55:06.138] Description:
[17:55:06.138] (luaDoTransformItem) Item not found
And are you sure you are using a normal lever?
normal lever is 1945
That should fix the second error.

first error change...
Hi, i have on map 20 chest and i want to spawn items inside if i use switch, but only for 5 chest.
try this.
LUA:
local config = {
    max_items = 3, -- items in chest to reward
    min_items = 1,
    chest_count = 2, -- how many chests to rewards items into
    lever_reset_timer = 10 -- seconds
}

local chest_positions = {
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1}, -- {itemid, max_count}
        {1111, 1}, -- max_count for non-stackable items should always be 1.
        {1111, 1}  -- max_count for stackable items should be between 1 and 100
        }
    },
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1},
        {1111, 1},
        {1111, 1}
        }
    },
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1},
        {1111, 1},
        {1111, 1}
        }
    }
}

-- full credit to cbrm for these 2 functions
-- https://otland.net/threads/260642/#post-2520564
local function _getContainerFreeSlots(container)
    return getContainerCap(container) - getContainerSize(container)
end

local function getContainerFreeSlots(container)
    if not isContainer(container) then
        return 0
    end

    local t, v = {}, 0
    for i = 0, getContainerSize(container)-1 do
        local item = getContainerItem(container, i)
        if isContainer(item.uid) then
            table.insert(t, item.uid)
        end
    end

    for _, check in pairs(t) do
        v = v + _getContainerFreeSlots(check)
    end

    return v + _getContainerFreeSlots(container)
end

local function lever_reset(pos)
    doTransformItem(getTileItemById(pos, 1946).uid, 1945)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)

    -- check if lever is currently used
    if item.itemid == 1946 then
        return doPlayerSendTextMessage(cid, 22, "Wait for switch to reset.")
    end
 
    -- transform lever, and add reset
    doTransformItem(item.uid, item.itemid + 1)
    addEvent(lever_reset, config.lever_reset_timer * 1000, toPosition)
 
    -- randomize chests
    local random_numbers, chests = {}, #chest_positions
    local max_chest_count = config.chest_count <= chests and config.chest_count or chests
    repeat
        local rand, count = math.random(chests), 0
        for i = 1, #random_numbers do
            if rand == random_numbers[i] then
                count = count + 1
            end
        end
        if count == 0 then
            table.insert(random_numbers, rand)
        end
    until #random_numbers == max_chest_count
 
    -- put items into the random chests
    for i = 1, #random_numbers do
        local index = chest_positions[random_numbers[i]]
        local chest, rand = getTileItemById(index[1], index.chest_itemid).uid, math.random(config.min_items, config.max_items)
        -- check to make sure chest is not missing
        if isContainer(chest) then
            -- make sure there is room for items
            local slots_available = getContainerFreeSlots(chest)
            if slots_available > 0 then
                -- only add items up to the containers limit
                rand = rand > slots_available and slots_available or rand
                local item_index = index.possible_items_to_reward_in_chest
                for i = 1, rand do
                    -- choose a random item each time, duplicate items are possible
                    local x = math.random(#item_index)
                    doAddContainerItem(chest, item_index[x][1], math.random(item_index[x][2]))
                end
            end
        end
    end
    return true
end
 
Last edited by a moderator:
function needed: [TFS 1.0] Position.iterateArea

Code:
local chests = []
Position.iterateArea(function(position)
local chest = Tile(position):getItemById(chestId)
if chest then
table.insert(chests, chest)
end
end, startPos, endPos)

then choose 5 random chests
 
try this.
LUA:
local config = {
    max_items = 3, -- items in chest to reward
    min_items = 1,
    chest_count = 2, -- how many chests to rewards items into
    lever_reset_timer = 10 -- seconds
}

local chest_positions = {
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1}, -- {itemid, max_count}
        {1111, 1}, -- max_count for non-stackable items should always be 1.
        {1111, 1}  -- max_count for stackable items should be between 1 and 100
        }
    },
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1},
        {1111, 1},
        {1111, 1}
        }
    },
    {{x = 1000, y = 1000, z = 7}, chest_itemid = 1111, possible_items_to_reward_in_chest = {
        {1111, 1},
        {1111, 1},
        {1111, 1}
        }
    }
}

-- full credit to cbrm for these 2 functions
-- https://otland.net/threads/260642/#post-2520564
local function _getContainerFreeSlots(container)
    return getContainerCap(container) - getContainerSize(container)
end

local function getContainerFreeSlots(container)
    if not isContainer(container) then
        return 0
    end

    local t, v = {}, 0
    for i = 0, getContainerSize(container)-1 do
        local item = getContainerItem(container, i)
        if isContainer(item.uid) then
            table.insert(t, item.uid)
        end
    end

    for _, check in pairs(t) do
        v = v + _getContainerFreeSlots(check)
    end

    return v + _getContainerFreeSlots(container)
end

local function lever_reset(pos)
    doTransformItem(getTileItemById(pos, 1946).uid, 1945)
end

function onUse(cid, item, fromPosition, itemEx, toPosition)

    -- check if lever is currently used
    if item.itemid == 1946 then
        return doPlayerSendTextMessage(cid, 22, "Wait for switch to reset.")
    end
 
    -- transform lever, and add reset
    doTransformItem(item.uid, item.itemid + 1)
    addEvent(lever_reset, config.lever_reset_timer * 1000, toPosition)
 
    -- randomize chests
    local random_numbers, chests = {}, #chest_positions
    local max_chest_count = config.chest_count <= chests and config.chest_count or chests
    repeat
        local rand, count = math.random(chests), 0
        for i = 1, #random_numbers do
            if rand == random_numbers[i] then
                count = count + 1
            end
        end
        if count == 0 then
            table.insert(random_numbers, rand)
        end
    until #random_numbers == max_chest_count
 
    -- put items into the random chests
    for i = 1, #random_numbers do
        local index = chest_positions[random_numbers[i]]
        local chest, rand = getTileItemById(index[1], index.chest_itemid).uid, math.random(config.min_items, config.max_items)
        -- check to make sure chest is not missing
        if isContainer(chest) then
            -- make sure there is room for items
            local slots_available = getContainerFreeSlots(chest)
            if slots_available > 0 then
                -- only add items up to the containers limit
                rand = rand > slots_available and slots_available or rand
                local item_index = index.possible_items_to_reward_in_chest
                for i = 1, rand do
                    -- choose a random item each time, duplicate items are possible
                    local x = math.random(#item_index)
                    doAddContainerItem(chest, item_index.x[1], math.random(item_index.x[2]))
                end
            end
        end
    end
    return true
end

[17:54:56.138] [Error - Action Interface]
[17:54:56.138] data/actions/scripts/chestspawn.lua:onUse
[17:54:56.138] Description:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: attempt to index field 'x' (a nil value)
[17:54:56.138] stack traceback:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: in function <data/actions/scripts/chestspawn.lua:119>

[17:55:06.138] [Error - Action Interface]
[17:55:06.138] In a timer event called from:
[17:55:06.138] data/actions/scripts/chestspawn.lua:onUse
[17:55:06.138] Description:
[17:55:06.138] (luaDoTransformItem) Item not found
 
[17:54:56.138] [Error - Action Interface]
[17:54:56.138] data/actions/scripts/chestspawn.lua:eek:nUse
[17:54:56.138] Description:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: attempt to index field 'x' (a nil value)
[17:54:56.138] stack traceback:
[17:54:56.138] data/actions/scripts/chestspawn.lua:160: in function <data/actions/scripts/chestspawn.lua:119>

[17:55:06.138] [Error - Action Interface]
[17:55:06.138] In a timer event called from:
[17:55:06.138] data/actions/scripts/chestspawn.lua:eek:nUse
[17:55:06.138] Description:
[17:55:06.138] (luaDoTransformItem) Item not found
And are you sure you are using a normal lever?
normal lever is 1945
That should fix the second error.

first error change
LUA:
doAddContainerItem(chest, item_index.x[1], math.random(item_index.x[2]))
to
LUA:
doAddContainerItem(chest, item_index[x][1], math.random(item_index[x][2]))
 
Last edited by a moderator:
Solution
Back
Top