I am trying to create an inventory system and I need to know if there is more than one of the same item in the inventory so the inventory can look functional and organized.
This is what I have tried so far:
inventoryEvent.OnClientEvent:Connect(function(ItemName) local count = 0 for _,v in pairs(player.Inventory:GetChildren()) do if v.Name == ItemName and count > 1 then print("There is more than one.") count = count + 1 end end
But this does not work because every time inventoryEvent
is fired then count
will be set back to 0. So it seems I somehow need to make the script remember what the count was for the item.
I am thinking I can do that by adding a NumberValue
under the item added to the inventory and changing the value each time a duplicate of the item is added to the inventory. But I still need the script be able to detect when there is more than one of the same item in the inventory.
How can I accomplish this?
I mean, you could use a NumberValue object, but that's adding an unnecessary layer of complexity and things to keep track of.
Instead of using NumberValues and for
loops, it would be much nicer to create a dictionary table that keeps track of which items the user has, and how many of them. Here's an implementation:
-- a dictionary record of the items the user has received local itemsOwned = { --[[ Example data: ["Item one"] = 3, -- user has 3 of these ["Item two"] = 7, -- user has 7 of these ["Item three"] = 0, -- user has 0 of these ]] } inventoryEvent.OnClientEvent:Connect(function(ItemName) -- get item data from the table if it exists local itemData = itemsOwned[itemName] -- case where user receives item for the first time if not itemData then -- the user now has 1 of this item itemsOwned[itemName] = 1 print("The user has received a new item!") -- case where user already has the item elseif itemData > 0 then itemsOwned[itemName] = itemData + 1 print("The user already has this item!") end end)
So, upon receiving a new item for the first time, that item name will not exist in the itemsOwned
table.
... -- get item data from the table if it exists local itemData = itemsOwned[itemName] -- case where user receives item for the first time if not itemData then ...
We then create a new entry for it and set it equal to 1
(to represent that the user owns only one of those items).
-- the user now has 1 of this item itemsOwned[itemName] = 1 print("The user has received a new item!")
If the user already owns the item, that will trigger the else
clause and bring us to the next branch of our code:
-- case where user already has the item elseif itemData > 0 then
Since the user already owns the item, itemData
already exists and we can simply increment it's value.
itemsOwned[itemName] = itemData + 1 print("The user already has this item!")
Hopefully this makes sense. Let me know if you have any questions.
I think because you added and count > 1
in the condition, the count += 1
never runs, therefore the count stays 0, simply remove that condition.
inventoryEvent.OnClientEvent:Connect(function(itemName) local count = 0 for _, item in pairs(player.Inventory:GetChildren()) do if v.Name == itemName then count += 1 end end if count > 1 then --there is more than one of the same item in the inventory end end)