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

Lua Remove an item by count from a specific tile

Obito

0x1337
Joined
Feb 17, 2011
Messages
350
Solutions
8
Reaction score
144
Location
Egypt
Hi, I was trying to remove an item with a heavy count of stacking items. I need to remove the item by count from the table on this element but it's not working as intended.
Lua:
local items_to_check = {
  {id = 7372, count = 2000},
}

local TILE_POSITIONS = {
    {x = 996, y = 996, z = 7},
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
  for i = 1, #TILE_POSITIONS do
    local tile = Tile(TILE_POSITIONS[i].x, TILE_POSITIONS[i].y, TILE_POSITIONS[i].z)
    if tile then
      for j = 1, #items_to_check do
        local item = tile:getItemById(items_to_check[j].id)
        if item then
          item:remove()
        end
      end
    end
  end
end
so the previous code ^ only removes 100 items and I don't know the reason
Also, I tried to use
Code:
item:remove(items_to_check[j].count)
to remove the count instead It also didn't work is there any idea that I can achieve this?
 
Solution
whipped this up for you.

usage
Lua:
removeItemsFromPositionByAmount(Position(1000, 1000, 7), 7372, 2000)
function
Lua:
local function removeItemsFromPositionByAmount(position, itemIdToFind, amount)
    local tile = Tile(position)
    local tileItems = tile:getItems()
  
    local count = 0
    local tileItemsWithId = {}
  
    -- counting items on tile, to verify there is enough to remove
    for i = 1, #tileItems do
        local tileItem = tileItems[i]
        if tileItem:getId() == itemIdToFind then
            tileItemsWithId[#tileItemsWithId + 1] = tileItem
            local itemCount = tileItem:getCount()
            count = count + itemCount
            if count >= amount then
                break
            end
        end...
whipped this up for you.

usage
Lua:
removeItemsFromPositionByAmount(Position(1000, 1000, 7), 7372, 2000)
function
Lua:
local function removeItemsFromPositionByAmount(position, itemIdToFind, amount)
    local tile = Tile(position)
    local tileItems = tile:getItems()
  
    local count = 0
    local tileItemsWithId = {}
  
    -- counting items on tile, to verify there is enough to remove
    for i = 1, #tileItems do
        local tileItem = tileItems[i]
        if tileItem:getId() == itemIdToFind then
            tileItemsWithId[#tileItemsWithId + 1] = tileItem
            local itemCount = tileItem:getCount()
            count = count + itemCount
            if count >= amount then
                break
            end
        end
    end
  
    if count < amount then
        print("Could not find enough items to remove. Count of item on tile is " .. count)
        return false
    end
  
    -- removing the specified amount of items
    local removedCount = 0
    for i = 1, #tileItemsWithId do
        local tileItem = tileItemsWithId[i]
        local itemCount = tileItem:getCount()
        if removedCount + itemCount < amount then
            tileItem:remove()
            removedCount = removedCount + itemCount
        else
            local amountToRemove = amount - removedCount
            tileItem:remove(amountToRemove)
            removedCount = amount
        end
    end
  
    print("Removed " .. removedCount .. " items from tile.")
    return true
end
 
Last edited:
Solution
whipped this up for you.

usage
Lua:
removeItemsFromPositionByAmount(Position(1000, 1000, 7), 7372, 2000)
function
Lua:
local function removeItemsFromPositionByAmount(position, itemIdToFind, amount)
    local tile = Tile(position)
    local tileItems = tile:getItems()
 
    local count = 0
    local tileItemsWithId = {}
 
    -- counting items on tile, to verify there is enough to remove
    for i = 1, #tileItems do
        local tileItem = tileItems[i]
        if tileItem:getId() == itemIdToFind then
            [#tileItemsWithId + 1] = tileItem
            local itemCount = tileItem:getCount()
            count = count + itemCount
            if count >= amount then
                break
            end
        end
    end
 
    if count < amount then
        print("Could not find enough items to remove. Count of item on tile is " .. count)
        return false
    end
 
    -- removing the specified amount of items
    local removedCount = 0
    for i = 1, #tileItemsWithId do
        local tileItem = tileItemsWithId[i]
        local itemCount = tileItem:getCount()
        if removedCount + itemCount < amount then
            tileItem:remove()
            removedCount = removedCount + itemCount
        else
            local amountToRemove = amount - removedCount
            tileItem:remove(amountToRemove)
            removedCount = amount
        end
    end
 
    print("Removed " .. removedCount .. " items from tile.")
    return true
end
Thank you @Xikini but my apologies, should I add this to my global.lua file? it gives me some errors
Rich (BB code):
Lua Script Error: [Main Interface]
data/global.lua
data/new_functions/remove_count.lua:12: unexpected symbol near '['
stack traceback:
        [C]: at 0x7ff68758cec0
        [C]: in function 'dofile'
        data/global.lua:6: in main chunk
Edit:- I already added it to my main script file and it works fine!
just changed line 12 from
Code:
[#tileItemsWithId + 1] = tileItem
to
Code:
 tileItemsWithId[#tileItemsWithId + 1] = tileItem
and it works fine! but for some reason, it doesn't work when I add it to global.lua
it prints this in the console while I try to use the script
Rich (BB code):
Lua Script Error: [Action Interface]
data/actions/scripts/test.lua:onUse
data/actions/scripts/test.lua:14: attempt to call global 'removeItemsFromPositionByAmount' (a nil value)
stack traceback:
        [C]: in function 'removeItemsFromPositionByAmount'
        data/actions/scripts/test.lua:14: in function <data/actions/scripts/test.lua:9>
 
Last edited:
I edited my post there's another problem if I'm trying to use it in global to make it easier
Replace:
Code:
local function removeItemsFromPositionByAmount
with:
Code:
function removeItemsFromPositionByAmount
local makes variable/function available only in given file (in which it's defined).
 
Replace:
Code:
local function removeItemsFromPositionByAmount
with:
Code:
function removeItemsFromPositionByAmount
local makes variable/function available only in given file (in which it's defined).
Thank you so much for your help. I really appreciate it @Gesior.pl works fine 🫡🫡 , Again wanted to extend a big thank you to @Xikini and @Roddet for your invaluable assistance. Your support has been greatly appreciated. :)
 
In Line 3, dont need to check If tile exist?
If tile and tile:getItems()

What happens If tile dont exist and this funcion run? Can server crash?
 
Back
Top