[Help] Get player total inventory items count

Hey!

The title says almost everything, but, i want to display the total item count on the player inventory.
Any ideas?

Thank you!

Probably would be nice to tell everyone what inventory you are using…???

1 Like

I believe he is just wanting to count the amount of items on a player.

1 Like

Still his question is missing information that his question will be unanswerable without the details.

1 Like

I’m sorry, forgot to say that.
I’m using the inventory that comes with ESX.
Thanks!

BUMP

/20charactersss

Something like this. This script assumes that xPlayer is the player you want to get the inventory count of… you can assign a player as xPlayer through a number of ways, see: https://github.com/ESX-Org/es_extended/wiki/Server-Functions and https://github.com/ESX-Org/es_extended/wiki/xPlayer for help.

local inventoryNumber = 0
for i=1, #xPlayer.inventory, 1 do
    inventoryNumber = inventoryNumber + 1
end
print(xPlayer.playerName .. " has " .. inventoryNumber .. " items in their inventory.")
1 Like

Worked as a charm.

I was doing it wrong.

Thank you!

Actually, rethinking my answer… You probably don’t need that loop. I hadn’t had my coffee yet…

local inventoryNumber = #xPlayer.inventory

That should work too, I believe.

How it’s supposed to be used?
It will retrive the total item quantity or make an array with all player items?

xPlayer.inventory is an array (or table…) of all of the inventory items that xPlayer has. The # operator is essentially a count function. So by saying #xPlayer.inventory , you are saying count(xPlayer.inventory), which would be a function that returns how many items are in the inventory.

So it retrieves an array (or table…) of the inventory and then counts them and returns an integer value of how many items the player has.

1 Like

Awesome man, thank you!