And are you sure you are using a normal lever?[17:54:56.138] [Error - Action Interface]
[17:54:56.138] data/actions/scripts/chestspawn.luanUse
[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.luanUse
[17:55:06.138] Description:
[17:55:06.138] (luaDoTransformItem) Item not found
try this.Hi, i have on map 20 chest and i want to spawn items inside if i use switch, but only for 5 chest.
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
local chests = []
Position.iterateArea(function(position)
local chest = Tile(position):getItemById(chestId)
if chest then
table.insert(chests, chest)
end
end, startPos, endPos)
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
And are you sure you are using a normal lever?[17:54:56.138] [Error - Action Interface]
[17:54:56.138] data/actions/scripts/chestspawn.luanUse
[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.luanUse
[17:55:06.138] Description:
[17:55:06.138] (luaDoTransformItem) Item not found
doAddContainerItem(chest, item_index.x[1], math.random(item_index.x[2]))
doAddContainerItem(chest, item_index[x][1], math.random(item_index[x][2]))