• 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 item with actionid on my backpack

nefinoo

Carnage.flv
Joined
Sep 11, 2010
Messages
554
Solutions
1
Reaction score
61
Location
Lo Mochis, Sinaloa
Using a example, i have a key with actionid = 1234 and the same key without actionid, how can remove the onlyone with actionid,
both keys has the itemid = 2973 (Bone key)
 
Untested..

But basically it loops through all the equipment slots of the player, and checks for the itemId and actionId combo.
If any of those items is a container, it checks all the items inside as well.

Lua:
-- example
local key = findItemOnPlayerWithActionId(player, 2973, 1234)
if key then
    -- key found, do something
    key:remove(1)
end
Lua:
-- potentially very heavy function, if player has many many items on them.
local function findItemOnPlayerWithActionId(player, itemId, actionId)
    if not player:isPlayer() then
        return false
    end
    for slot = 1, 10 do
        local slotItem = player:getSlotItem(slot)
        if slotItem then
            local slotItemId = slotItem:getId()
            if slotItemId == itemId and slotItem:getActionId() == actionId then
                return slotItem
            end
            if ItemType(slotItemId):isContainer() then
                local containerItems = slotItem:getItems(true)
                for i = 1, #containerItems do
                    local item = containerItems[i]
                    if item:getId() == itemId and item:getActionId() == actionId then
                        return item
                    end
                end
            end
        end
    end
    return false
end
 
Untested..

But basically it loops through all the equipment slots of the player, and checks for the itemId and actionId combo.
If any of those items is a container, it checks all the items inside as well.

Lua:
-- example
local key = findItemOnPlayerWithActionId(player, 2973, 1234)
if key then
    -- key found, do something
    key:remove(1)
end
Lua:
-- potentially very heavy function, if player has many many items on them.
local function findItemOnPlayerWithActionId(player, itemId, actionId)
    if not player:isPlayer() then
        return false
    end
    for slot = 1, 10 do
        local slotItem = player:getSlotItem(slot)
        if slotItem then
            local slotItemId = slotItem:getId()
            if slotItemId == itemId and slotItem:getActionId() == actionId then
                return slotItem
            end
            if ItemType(slotItemId):isContainer() then
                local containerItems = slotItem:getItems(true)
                for i = 1, #containerItems do
                    local item = containerItems[i]
                    if item:getId() == itemId and item:getActionId() == actionId then
                        return item
                    end
                end
            end
        end
    end
    return false
end
knight with level 200 and loads of backpacks:
challenge accepted training GIF
 
Feel free to post a cleaner solution.
just add check to check only first container and give error that it was not found in first backback instead of infinitely iterating over everything and using server res? or yet better to make the action smoother make sure player has it in his hand it only makes sense the player is "handing" the key right? pure RPG vibe
 
just add check to check only first container and give error that it was not found in first backback instead of infinitely iterating over everything and using server res? or yet better to make the action smoother make sure player has it in his hand it only makes sense the player is "handing" the key right? pure RPG vibe
It all depends on what the OP requires. If he needs the key deleted no matter what, then recursion is the only option.
 
It all depends on what the OP requires. If he needs the key deleted no matter what, then recursion is the only option.
yeah but if the recursion doesnt finish and player removes backpacks /trades them etc? or changes positions in bp
 
miliseconds until its evo ots with 999999cap
Very true. But then it comes down to not allowing mis-use of the script, i.e adding exhausts etc.

Remember that methods like getItemCountById use the same type of recursion (c++ is faster than lua, but the principles are the same)
 
For the whiners who want a function that allows for every situation.. instead of just what the OP wanted..
Lua:
-- potentially very heavy function, if player has many many items on them.
-- set deepSearch to false, to only check the top layer of containers.
local function findItemOnPlayerWithActionId(player, itemId, actionId, deepSearch)
    if not player:isPlayer() then
        return false
    end
    deepSearch = deepSearch == nil and true or deepSearch
    for slot = 1, 10 do
        local slotItem = player:getSlotItem(slot)
        if slotItem then
            local slotItemId = slotItem:getId()
            if slotItemId == itemId and slotItem:getActionId() == actionId then
                return slotItem
            end
            if ItemType(slotItemId):isContainer() then
                local containerItems = slotItem:getItems(deepSearch)
                for i = 1, #containerItems do
                    local item = containerItems[i]
                    if item:getId() == itemId and item:getActionId() == actionId then
                        return item
                    end
                end
            end
        end
    end
    return false
end
 
Back
Top