• 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: Don't allow to carry more than certain amount of an item

bizao030188

Member
Joined
Jun 4, 2012
Messages
50
Solutions
2
Reaction score
7
Hi!
I want to create a script that does not allow a player carry more than, let's say 3 items of id 100. I did this on player events:

Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    print("onMoveItem")
    if not item.getId() == 100 then return true end
    local player = Player(toCylinder)
    if player then      
        local count = player:getItemCount(100)
        print(player:getName())
        print(count)
        if count > 3 then player:sendCancelMessage("Sorry, not possible. You are at maximum capacity") return false end
    end  
    return true
end

The problem is that "if player then" condition is accessed only when I equip with the item but not when I put it in any bag the player is holding. I want it to check everywhere, what should I change?

I am using TFS 1.2

Thanks in advanced!
 
Solution
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local itemid = 2160
    local maxcount = 50
    if toPosition.x == 65535 then
        if item:isContainer() then
            count = searchInContainer(item, itemid)
        end
        if (item:getId() == itemid or item:isContainer()) and self:getItemCount(itemid)+count > maxcount then
            self:sendCancelMessage("Sorry, not possible. You are at maximum capacity")
            return false
        end
    end
    return true
end

function searchInContainer(container, itemId)
    local count = 0
    if container:isContainer() then
        local items = container:getItem()
        for i = 0, container:getSize() do
            local...
Perhaps you could try out:

Metatable Item:
getUniqueId()
getParent()
getTopParent()

Metatable Player:
getSlotItem(slot)

To figure out of the destination is inside any equipped containers by comparing unique id of top parent and destination top parent?

Could this be useful? (perhaps its already added in some other form)
Lua:
function Player:hasItem(item)
    local top_item = item:getTopParent()
    for i = 1, 10, 1 do
        local slot_item = self:getSlotItem(i)
        if slot_item ~= nil then
            if slot_item:getUniqueId() == top_item:getUniqueId() then
                return true
            end
        end
    end
    return false
end
 
Perhaps you could try out:

Metatable Item:
getUniqueId()
getParent()
getTopParent()

Metatable Player:
getSlotItem(slot)

To figure out of the destination is inside any equipped containers by comparing unique id of top parent and destination top parent?

Could this be useful? (perhaps its already added in some other form)
Lua:
function Player:hasItem(item)
    local top_item = item:getTopParent()
    for i = 1, 10, 1 do
        local slot_item = self:getSlotItem(i)
        if slot_item ~= nil then
            if slot_item:getUniqueId() == top_item:getUniqueId() then
                return true
            end
        end
    end
    return false
end

Hi @Znote, thanks for answering. Actually, I was trying to use the functions you mentioned but I am a little bit confused about how they work. Thanks for the script, I will try it out.
 
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local itemid = 2160
    local maxcount = 50
    if toPosition.x == 65535 then
        if item:isContainer() then
            count = searchInContainer(item, itemid)
        end
        if (item:getId() == itemid or item:isContainer()) and self:getItemCount(itemid)+count > maxcount then
            self:sendCancelMessage("Sorry, not possible. You are at maximum capacity")
            return false
        end
    end
    return true
end

function searchInContainer(container, itemId)
    local count = 0
    if container:isContainer() then
        local items = container:getItem()
        for i = 0, container:getSize() do
            local item = container:getItem(i)
            if item then
                if item:isContainer() then
                    if item:getId() == itemId then
                        count = count + item:getCount()
                    end
                    count = count + searchInContainer(item, itemId)
                elseif item:getId() == itemId then
                    count = count + item:getCount()
                end
            end
        end
    end
    return count
end
edit: added container check so players cant equip bps with more items than allowed
 
Last edited:
Solution
Lua:
function Player:onMoveItem(item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    local itemid = 2160
    local maxcount = 50
    if toPosition.x == 65535 then
        if item:isContainer() then
            count = searchInContainer(item, itemid)
        end
        if (item:getId() == itemid or item:isContainer()) and self:getItemCount(itemid)+count > maxcount then
            self:sendCancelMessage("Sorry, not possible. You are at maximum capacity")
            return false
        end
    end
    return true
end

function searchInContainer(container, itemId)
    local count = 0
    if container:isContainer() then
        local items = container:getItem()
        for i = 0, container:getSize() do
            local item = container:getItem(i)
            if item then
                if item:isContainer() then
                    if item:getId() == itemId then
                        count = count + item:getCount()
                    end
                    count = count + searchInContainer(item, itemId)
                elseif item:getId() == itemId then
                    count = count + item:getCount()
                end
            end
        end
    end
    return count
end
edit: added container check so players cant equip bps with more items than allowed


Thank you very much!
I was working on your first answer to add container check and found out a solution without for loops:

Lua:
    if toPosition.x == 65535 then
        if item:isContainer() then
            count = item:getItemCountById(itemid)
        end
 
Back
Top