• 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 Check if player have space on backpack

massuco

Brazilian, sorry for my bad english XD
Joined
Feb 17, 2013
Messages
199
Solutions
8
Reaction score
22
Location
Brasil
I am using this script trying to check the space in backpack, if have free space, it receive a reward, if not, a message with "you have no room etc.." But i dont know how to use the function to check the room on backpack. PLEASE, someone help me?
I was trying using:
local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
if (getContainerCap(bp.uid) ~= 0) then
but dont works
Code:
function onUse(cid, item, frompos, item2, topos)
    if item.uid == 50124 then
    queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if (getContainerCap(bp.uid) ~= 0) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
            
        else
        doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
    return 0
    end
return 1
end
 
Containers can have their own Capacity, that isn't related to slots.

For example, a container can have 4 slots, but only able to hold 30 cap.
So you could throw 4 torches in there, but not 400 gold coins.

You'll need a different function to check container slots to see if there are any empty slots.

Which is pretty easy.

Simply:
Code:
function onUse(cid, item, frompos, item2, topos)
   if item.uid == 50124 then
    queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if (getContainerItem(bp.uid, getContainerSize(dp.uid)-1).itemid ~= 0) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
          
        else
        doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
    return 0
    end
return 1
end
 
I've tested it, now it only give a reward if you have a container full empty.
Example, if I have a bag with 7 free slots, it dont give a reward. BUT if I have a bag with 8 free slots (full free) it give a reward.
@Flatlander
 
That is strange...
The way getContainerItem(uid, slot) works is, it gets the item that is in the slot you ask.

For example,
Code:
getContainerItem(uid, 0)
should get the first slot.

This should get the last slot. (See if container is full)
Code:
getContainerItem(uid, getContainerSize(uid)-1)

**EDIT** Found my issue, I put dp.uid instead of bp.uid (so it was returning 0)
Try the below, it should work. (Make sure it works 100%)
Code:
function onUse(cid, item, frompos, item2, topos)
  if item.uid == 50124 then
    queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if (getContainerItem(bp.uid, getContainerSize(bp.uid)-1).itemid ~= 0) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
         
        else
        doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
    return 0
    end
return 1
end
 
I saw the error "dp" instead "bp" and changed it, but it dont works. Only give a reward if I have a backpack with full free spaces.
@Flatlander
 
Use this and paste what your character says.
Code:
function onUse(cid, item, frompos, item2, topos)
  if item.uid == 50124 then
    queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
           local size = getContainerSize(dp.uid)
           local str = "BP Size: "..size..""
           for a = 0, size do
               local CheckItem = getContainerItem(bp.uid, a)
               str = str.."\nItem "..a..": "..CheckItem.itemid..""
           end
           doCreatureSay(cid, str, 1)
            if (getContainerItem(bp.uid, getContainerSize(bp.uid)-1).itemid ~= 0) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
         
        else
        doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
    return 0
    end
return 1
end
 
I saw the error "dp" instead "bp" and changed it, but it dont works. Only give a reward if I have a backpack with full free spaces.
@Flatlander
Code:
function onUse(cid, item, frompos, item2, topos)
    if item.uid == 50124 then
        queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if getContainerSize(bp.uid) == getContainerCap(bp.uid) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end 
        else
            doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
        return false
    end
    return true
end
try this?
container size gets the current number of items inside the container, and container cap is the max amount of slots it has
 
20:17 Cebola: BP Size: 4
Item 0: 2666
Item 1: 2666
Item 2: 2666
Item 3: 2666
Item 4: 0
20:17 You have no room

I have a bag with 4 meats, and 4 free slots
@Flatlander
 
Your bag has 8 slots? and it is saying it has 4 slots?

That is very strange.... maybe that function does not work how I thought it did.

Try this:
Code:
function onUse(cid, item, frompos, item2, topos)
  if item.uid == 50124 then
    queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
           local size = getContainerSize(dp.uid)
           local str = "BP Size: "..size..""
           for a = 0, size do
               local CheckItem = getContainerItem(bp.uid, a)
               str = str.."\nItem "..a..": "..CheckItem.itemid..""
           end
           doCreatureSay(cid, str, 1)
            if (getContainerItem(bp.uid, getContainerSize(bp.uid)).itemid ~= 0) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
         
        else
        doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
    return 0
    end
return 1
end
 
IT WORKED XERAPHUS
@Xeraphus Very Thanks, I have been trying to make it work since 2 days.. Im newbie in lua.
@Flatlander Very thanks for you too for your attention. The script of Xeraphus worked nice.
 
Code:
function onUse(cid, item, frompos, item2, topos)
    if item.uid == 50124 then
        queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if getContainerSize(bp.uid) == getContainerCap(bp.uid) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
        else
            doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
        return false
    end
    return true
end
try this?
container size gets the current number of items inside the container, and container cap is the max amount of slots it has

I was very wrong at how those functions work. Haha that's what I get for just reading them and "assuming" what they do.
I do know you can limit the capacity a container can hold, I assumed getContainerCap(uid) checked this number.

//Embarrased
 
Code:
function onUse(cid, item, frompos, item2, topos)
    if item.uid == 50124 then
        queststatus = getPlayerStorageValue(cid,50125)
        if queststatus == -1 then
            local bp = getPlayerSlotItem(cid, CONST_SLOT_BACKPACK)
            if getContainerSize(bp.uid) == getContainerCap(bp.uid) then
                doPlayerSendTextMessage(cid,22,"You have no room")
            else
                if (getPlayerFreeCap(cid) >= 80) then
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance.")
                doPlayerAddItem(cid, 2425, 1)
                setPlayerStorageValue(cid,50125,1)
                else
                doPlayerSendTextMessage(cid,22,"You have found a obsidian lance. Weighing 80.00 oz it is too heavy.")
                end
            end
        else
            doPlayerSendTextMessage(cid,22,"The dead human is empty.")
        end
    else
        return false
    end
    return true
end
try this?
container size gets the current number of items inside the container, and container cap is the max amount of slots it has


hey man, your code to check the BP slots and give an error mensage if the slots are full are awesome, but if i have another bag in some of the slots, it keeps saying that i dont have room on it, can you help me to work on it?
 
Back
Top