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

TFS 1.X+ Check items container onTrade

CastorFlynn

Member
Joined
Aug 29, 2021
Messages
88
Reaction score
8
How do I check items inside containers to see if they have an AID assigned? I would like to do this check within the onTradeRequest function and block if player tried trade a bag/parcel/backpack with some item inside with AID.
 
Solution
Lua:
local targetAids = {
    1000,
    1001,
    1002
}

local ec = EventCallback
function ec.onTradeRequest(player, target, item)

    if item:getActionId() > 0 and isInArray(targetAids, item:getActionId()) then
        player:sendCancelMessage("Your item cannot be traded.")
        return false
    end

    if item:isContainer() then
        local containerItems = item:getItems(true)
       
        for _, subItem in pairs(containerItems) do -- # we want to avoid linear search
            if subItem:getActionId() > 0 and isInArray(targetAids, subItem:getActionId()) then
                player:sendCancelMessage("Your container contains items that cannot be traded.")
                return false
            end
        end
    end...
Lua:
local targetAids = {
    1000,
    1001,
    1002
}

local ec = EventCallback
function ec.onTradeRequest(player, target, item)

    if item:getActionId() > 0 and isInArray(targetAids, item:getActionId()) then
        player:sendCancelMessage("Your item cannot be traded.")
        return false
    end

    if item:isContainer() then
        local containerItems = item:getItems(true)
       
        for _, subItem in pairs(containerItems) do -- # we want to avoid linear search
            if subItem:getActionId() > 0 and isInArray(targetAids, subItem:getActionId()) then
                player:sendCancelMessage("Your container contains items that cannot be traded.")
                return false
            end
        end
    end

    return true
end
ec:register()
 
Solution
Lua:
local targetAids = {
    1000,
    1001,
    1002
}

local ec = EventCallback
function ec.onTradeRequest(player, target, item)

    if item:getActionId() > 0 and isInArray(targetAids, item:getActionId()) then
        player:sendCancelMessage("Your item cannot be traded.")
        return false
    end

    if item:isContainer() then
        local containerItems = item:getItems(true)
      
        for _, subItem in pairs(containerItems) do -- # we want to avoid linear search
            if subItem:getActionId() > 0 and isInArray(targetAids, subItem:getActionId()) then
                player:sendCancelMessage("Your container contains items that cannot be traded.")
                return false
            end
        end
    end

    return true
end
ec:register()
Work! Thank you!
 
Back
Top