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

[Function] Get all items (by id) / get all items in slot (by id)

Summ

(\/)(;,,;)(\/) Y not?
Staff member
Global Moderator
Joined
Oct 15, 2008
Messages
4,152
Solutions
12
Reaction score
1,107
Location
Germany :O
Since it might be useful and someone searched a function like this.
You can get all items by a certain id or get all items of a player by leaving "id" empty. => getAllItemsById(cid)

getAllItemsById(cid [, id])
-- returns an array of the items found like: ret = {{itemid = 2160, type = 1, etc}, {itemid = 2222, type = 1, etc}} -> storing the normal item arrays
-- if id is set in the parameter it will only returns those items with that id in the array
Code:
function getAllItemsById(cid, id)
    local containers = {}
    local items = {}
   
    for i = CONST_SLOT_FIRST, CONST_SLOT_LAST do
        local sitem = getPlayerSlotItem(cid, i)
        if sitem.uid > 0 then
            if isContainer(sitem.uid) then
                table.insert(containers, sitem.uid)
            elseif not(id) or id == sitem.itemid then
                table.insert(items, sitem)
            end
        end
    end

    while #containers > 0 do
        for k = (getContainerSize(containers[1]) - 1), 0, -1 do
            local tmp = getContainerItem(containers[1], k)
            if isContainer(tmp.uid) then
                table.insert(containers, tmp.uid)
            elseif not(id) or id == tmp.itemid then
                table.insert(items, tmp)
            end
        end
        table.remove(containers, 1)
    end

    return items
end

It will return an array with the items. Example:
Code:
for _, item in pairs(getAllItemsById(cid, 2222)) do
    doPlayerSay(cid, "I got an item with id " .. item.itemid .. ".")
end

getAllItemsBySlot(cid, slot [, id])
-- returns all items in a slot (return array same as above function)
-- especially useful to detect all backpack items
-- also id parameter is optional so you can only search a certain id in the backpack, for example get all his swords
Code:
function getAllItemsBySlot(cid, slot, id)
    local containers = {}
    local items = {}

    local sitem = getPlayerSlotItem(cid, i)
    if sitem.uid > 0 then
        if isContainer(sitem.uid) then
            table.insert(containers, sitem.uid)
        elseif not(id) or id == sitem.itemid then
            table.insert(items, sitem)
        end
    end

    while #containers > 0 do
        for k = (getContainerSize(containers[1]) - 1), 0, -1 do
            local tmp = getContainerItem(containers[1], k)
            if isContainer(tmp.uid) then
                table.insert(containers, tmp.uid)
            elseif not(id) or id == tmp.itemid then
                table.insert(items, tmp)
            end
        end
        table.remove(containers, 1)
    end

    return items
end

Modifications:
The script will not return the backpacks/containers it is scanning, incase you also want the backpacks returned in the arrays, add the following:

After:
table.insert(containers, sitem.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, sitem) end

After:
table.insert(containers, tmp.uid)
Add:
if not(id) or id == sitem.itemid then table.insert(items, tmp) end
 
Last edited:
No it creates a list of all items a player got.
 
So to use it its like.

if param == '3' then
doPlayerPopupFYI(cid, "Backpack: " .. getAllItemsById(cid, 3) .. "")

????????
 
So to use it its like.

if param == '3' then
doPlayerPopupFYI(cid, "Backpack: " .. getAllItemsById(cid, 3) .. "")

????????

No it gets all items the player is carrying. All slots + all stuff inside of backpacks (though it won't return the containers itself, however that can be added if someone needs it.

You would use it like this for example:
doPlayerSay(cid, "I am carrying " .. #getAllItemsById(cid) .. " items.") -- returns all items

or

doPlayerSay(cid, "I am carrying " .. #getAllItemsById(cid, 2160) .. " different crystal coin stacks." -- returns all stacks of goldcoins


For all items in a slot use the other script I posted in the main post.
 
Summ, this can cause lag if checks certain items of 100 players online?
Like 100 players online, check if exist 2 items with a certain attribute and delete both?
I did a script that delete item with a certain tid and the script works, but my doubt is if the script runnin 1 or 2 times per day with 100+ players online will cost too much memory?
Amazing function!
Thanks :)
 
Last edited:
Can this be used for auto stacking gold forexample ??

I need autostacking for avasta 7.6
 
Back
Top