[Help] Get player total inventory items count


#1

Hey!

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

Thank you!


#2

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


#3

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


#4

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


#5

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


#6

BUMP

/20charactersss


#7

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.")

#8

Worked as a charm.

I was doing it wrong.

Thank you!


#9

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.


#10

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


#11

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.


#12

Awesome man, thank you!