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

getWeight issue.

Joined
Mar 14, 2020
Messages
139
Solutions
3
Reaction score
11
Why this
Code:
getItemWeight(container.uid,1)
always return 0?
Already tried with
Code:
ItemType(container.uid):getWeight()
and getWeight(1) and this return 0 too.

Code:
if item:getType():isPickupable() then
        if toPosition.x == CONTAINER_POSITION then
        local container = getTileItemByType(toCylinder:getPosition(),ITEM_TYPE_CONTAINER)
            if container then
                if item:getWeight() + getItemWeight(container.uid,1) >= 999999 then
                    self:sendCancelMessage('You cannot put more itens in here.')
                    return false
                end
            end   
        end
    end

I'm trying to make impossible to a player put more itens inside a container that have 9.999 oz.

I did this one but i'm trying without an FOR:

Code:
if item:getType():isPickupable() then
        if toPosition.x == CONTAINER_POSITION then
        local container = Tile(toCylinder:getPosition()):getItems()
        local itemTemp
            for i = 1, #container do
                itemTemp = container[i]
                if ItemType(itemTemp:getId()):isContainer() then
                    if item:getWeight() + itemTemp:getWeight() >= 999999 then
                        self:sendCancelMessage('You cannot put more itens in here.')
                        return false
                    end
                break
                end
            end   
        end
    end
 
Last edited:
Why this getItemWeight(container.uid,1) always return 0?
Already tried with ItemType(container.uid):getWeight() and getWeight(1) and this return 0 too.

View attachment 56993

I'm trying to make impossible to a player put more itens inside a container that have 9.999 oz.

I did this one but i'm trying without an FOR:

View attachment 56994
 
Lua:
local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        if toCylinder and toCylinder:isItem() and toCylinder:getWeight() + item:getWeight() > 999 then
            return false
        end
    end
    return true
end

ec:register()
Tested and work!

What happens next is that all containers can only have a maximum weight of 9.99 and so this is very uncomfortable for everyone
Maybe if you do this for certain specific containers it would make more sense, or maybe a special container
 
Lua:
local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        if toCylinder and toCylinder:isItem() and toCylinder:getWeight() + item:getWeight() > 999 then
            return false
        end
    end
    return true
end

ec:register()
Tested and work!

What happens next is that all containers can only have a maximum weight of 9.99 and so this is very uncomfortable for everyone
Maybe if you do this for certain specific containers it would make more sense, or maybe a special container
Oh, but you know why the
Lua:
getItemWeight(container.uid,1)
Always return 0 weight?
Post automatically merged:

fixed.
Post automatically merged:

Lua:
local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        if toCylinder and toCylinder:isItem() and toCylinder:getWeight() + item:getWeight() > 999 then
            return false
        end
    end
    return true
end

ec:register()
Tested and work!

What happens next is that all containers can only have a maximum weight of 9.99 and so this is very uncomfortable for everyone
Maybe if you do this for certain specific containers it would make more sense, or maybe a special container
I tested and if a backpack is inside the container doesn't work, like:
Backpack 1: have: 1 backpack, this backpack have another inside, and we can have a backpack with 1000000oz.
 
Last edited:
Always return 0 weight?
function getItemWeight(itemId, ...) return ItemType(itemId):getWeight(...) / 100 end

because this function requires the ID of the item and not its UID
btw I recommend that you do not use the compat.lua functions, they were not created for scripting, it was only created to avoid rewriting scripts of old versions

Here is an example that works with all nested containers
Lua:
local getFirstContainer = function (container)
    while container:isItem() do
        local parent = container:getParent()
        if not parent or not parent:isItem() then
            return container
        end
        container = parent
    end
end

local ec = EventCallback

function ec.onMoveItem(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
    if toPosition.x == CONTAINER_POSITION then
        if toCylinder and toCylinder:isItem() then
            local fromContainer, container = getFirstContainer(fromCylinder), getFirstContainer(toCylinder)
            if container and (not fromContainer or fromContainer ~= container) and container:getWeight() + item:getType():getWeight(count) > 10000 then
                player:sendCancelMessage('You cannot put more itens in here.')
                return false
            end
        end
    end
    return true
end

ec:register()
 
Last edited:
Back
Top