• 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 Help with getItemHoldingCount()

Fortera Global

Intermediate OT User
Joined
Nov 20, 2015
Messages
1,180
Solutions
2
Reaction score
117
I want to prevent players from placing items when the backpack reaches 5 full slots, this script works to some extent, to cheat is just the player to open the backpack that is inside the other and move the item there, like getTopParent() no working.

tried onMove;
Lua:
local containerIdDP = toPosition.y - 64
        local containerDP = self:getContainerById(containerIdDP)
        if containerDP then
        local ItemDEE = containerDP:getTopParent()
        if ItemDEE and containerDP:getItemHoldingCount() >= 5 then
            self:sendCancelMessage("NOT POSSIBLE")
            self:getPosition():sendMagicEffect(CONST_ME_POFF)
            return false
        end
        end

fe6c16bbf743f5aa2387f9480f171cf2.gif


TFS 1.2
 
Solution
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        local container = self:getContainerById(toPosition.y - 64)
        if not container then
            if toPosition.y == CONST_SLOT_BACKPACK then
                container = self:getSlotItem(CONST_SLOT_BACKPACK)
            end
        end
        if container then
            local parent = toCylinder:getParent()
            local finalContainer = container
            if parent and parent:isItem() then
                local nextParent = parent:getParent()
                while nextParent and nextParent:isItem() do
                    parent = nextParent
                    nextParent =...
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        local container = self:getContainerById(toPosition.y - 64)
        if not container then
            if toPosition.y == CONST_SLOT_BACKPACK then
                container = self:getSlotItem(CONST_SLOT_BACKPACK)
            end
        end
        if container then
            local parent = toCylinder:getParent()
            local finalContainer = container
            if parent and parent:isItem() then
                local nextParent = parent:getParent()
                while nextParent and nextParent:isItem() do
                    parent = nextParent
                    nextParent = parent:getParent()
                end
                finalContainer = parent
            end
            if finalContainer:getItemHoldingCount() >= 5 then
                self:sendCancelMessage("NOT POSSIBLE")
                self:getPosition():sendMagicEffect(CONST_ME_POFF)
                return false
            end
        end
    end
    return true
end
 
Solution
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        local container = self:getContainerById(toPosition.y - 64)
        if not container then
            if toPosition.y == CONST_SLOT_BACKPACK then
                container = self:getSlotItem(CONST_SLOT_BACKPACK)
            end
        end
        if container then
            local parent = toCylinder:getParent()
            local finalContainer = container
            if parent and parent:isItem() then
                local nextParent = parent:getParent()
                while nextParent and nextParent:isItem() do
                    parent = nextParent
                    nextParent = parent:getParent()
                end
                finalContainer = parent
            end
            if finalContainer:getItemHoldingCount() >= 5 then
                self:sendCancelMessage("NOT POSSIBLE")
                self:getPosition():sendMagicEffect(CONST_ME_POFF)
                return false
            end
        end
    end
    return true
end

Thanks buddy, but I needed to check containers only on the floor, not on the player. And this method could not cause lags for some reason?

Oh my bad, this script holding the player right? I'll test <3
 
Last edited by a moderator:
Thanks buddy, but I needed to check containers only on the floor, not on the player. And this method could not cause lags for some reason?

Oh my bad, this script holding the player right? I'll test <3
if you're only checking for tiles then use this
also you rarely have to worry about performance since lua is so fast
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        local container = self:getContainerById(toPosition.y - 64)
        if container then
            local topParent = container:getTopParent()
            if topParent and topParent:isItem() and topParent:getItemHoldingCount() >= 5 then
                self:sendCancelMessage("NOT POSSIBLE")
                self:getPosition():sendMagicEffect(CONST_ME_POFF)
                return false
            end
        end
    end
    return true
end
 
if you're only checking for tiles then use this
also you rarely have to worry about performance since lua is so fast
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        local container = self:getContainerById(toPosition.y - 64)
        if container then
            local topParent = container:getTopParent()
            if topParent and topParent:isItem() and topParent:getItemHoldingCount() >= 5 then
                self:sendCancelMessage("NOT POSSIBLE")
                self:getPosition():sendMagicEffect(CONST_ME_POFF)
                return false
            end
        end
    end
    return true
end

Worked 100%, thanks!
Can you give your contact discord?
 
Back
Top