• 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 Little Help in Player.lua

viking

Member
Joined
Aug 20, 2015
Messages
323
Reaction score
22
Hello,
I'm trying to do when the player trying to put more than 10 items in the same sqm, not be possible. But it is giving me error (nil value in line 227).

Help?
Script:
6VxttYi.png


Code in Events/Scripts/Player.lua:
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
    if toPosition.x == CONTAINER_POSITION then
        local cid = toPosition.y - 64 -- container id
        local container = self:getContainerById(cid)   
        if not container then
            return true
        end

      local tileCount = Tile(toPosition):getItemCount()
       if tileCount > 10 then
          self:sendCancelMessage('Sorry, not possible.')
          return false
     end
       
        -- Do not let the player insert items into either the Reward Container or the Reward Chest
        local itemId = container:getId()   
        if itemId == ITEM_REWARD_CONTAINER or itemId == ITEM_REWARD_CHEST then
            self:sendCancelMessage('Sorry, not possible.')
            return false
        end

        -- The player also shouldn't be able to insert items into the boss corpse   
        local tile = Tile(container:getPosition())
        for _, item in ipairs(tile:getItems()) do
            if item:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == 2^31 - 1 and item:getName() == container:getName() then
                self:sendCancelMessage('Sorry, not possible.')
                return false
            end
        end
    end

    -- Do not let the player move the boss corpse.
    if item:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == 2^31 - 1 then
        self:sendCancelMessage('Sorry, not possible.')
        return false
    end

    return true
end
 
It seems like you have already received an answer in the Premium Support Board.

You placed the code in the if-clause checking for "toPosition.x == CONTAINER_POSITION".
Place it outside of that and check if the Tile() you are constructing exists before getting item count.
 
Okay. Check if this fixes your issue then.
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
    if toPosition.x == CONTAINER_POSITION then
        local cid = toPosition.y - 64 -- container id
        local container = self:getContainerById(cid) 
        if not container then
            return true
        end

        -- Do not let the player insert items into either the Reward Container or the Reward Chest
        local itemId = container:getId() 
        if itemId == ITEM_REWARD_CONTAINER or itemId == ITEM_REWARD_CHEST then
            self:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
            return false
        end

        -- The player also shouldn't be able to insert items into the boss corpse 
        local tile = Tile(container:getPosition())
        if tile then
            for _, item in ipairs(tile:getItems()) do
                if item:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == 2^31 - 1 and item:getName() == container:getName() then
                    self:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
                    return false
                end
            end
        end
    end

    local tile = Tile(toPosition)
    if tile and tile:getItemCount() > 10 then
        self:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
        return false
    end

    -- Do not let the player move the boss corpse.
    if item:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER) == 2^31 - 1 then
        self:sendCancelMessage(RETURNVALUE_NOTPOSSIBLE)
        return false
    end

    return true
end
 
Back
Top