Inventory system, loop iterates twice and creates double items

I’m in the middle of creating an inventory system and have come across an issue that I can’t seem to figure out.

Currently, as the player is added, I’m reading from my data store and applying any items inside said data store into the inventory GUI I created. However, it seems to want to create a double of one item that should be stacked, and I’m not sure what is causing it and how I could fix it.

Currently, the code is as follows:

print("time to load inventory")
	for _,item in pairs(data.Inventory) do
		print("iterating through data")
		if item.Amount == 1 then
			print("found item with amount 1: ".. item.Name)
			itemPanel = folderTemplate.item:Clone()
			itemPanel.Parent = playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList")
			itemPanel.Name = item.Name
			itemPanel.itemname.Text = item.Name
			itemPanel.itemamt.Text = item.Amount
			itemPanel.itemimg.Image = item.Image
			itemPanel.Visible = true
		elseif item.Amount >= 2 then
			print("found item with amount 2+: ".. item.Name)
			for _,v in playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList"):GetChildren() do
				if item.Name == v.Name then
					v.itemamt.Text = item.Amount
				else
					itemPanel = folderTemplate.item:Clone()
					itemPanel.Parent = playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList")
					itemPanel.Name = item.Name
					itemPanel.itemname.Text = item.Name
					itemPanel.itemamt.Text = item.Amount
					itemPanel.itemimg.Image = item.Image
					itemPanel.Visible = true
				end
			end
		end
	end

I added print to specific sections for debugging purposes, so the output logs are:

  02:23:30.376  time to load inventory  -  Server - DataService:56
  02:23:30.376  iterating through data  -  Server - DataService:58
  02:23:30.376  found item with amount 2+: Cute Face  -  Server - DataService:69
  02:23:30.376   ▶ iterating through data (x2)  -  Server - DataService:58
  02:23:30.376  found item with amount 2+: Scary Face  -  Server - DataService:69

Everything looks fine, but the loop somehow doubles up when iterating through the data, which I believe is what’s causing the output of two items when there should be a single, stacked item. Again, not sure why or how this is happening, but I’m hoping someone here has an explanation and can help.

EDIT: Okay, the reason for the double iteration at the end is because one of the values in the table contains an amount less than 1, so it’s not considered at all. However, still no clue why the script is continuing to create two guis for items that should be stacked.

EDIT 2: Figured out through more debugging that the script somehow calls for an item twice, somehow, even when specifying in the if,else statement that items not matching the name of existing inventory spots will have to create a new spot. No clue why that’s happening.

02:37:06.930   ▶ Scary Face is not in this inventory! create! (x2)  -  Server - DataService:75

I think you have two separate scary faces in your inventory’s save data, one with amount set to 1 and one with amount set to anything else. An item slot with an amount set to 1 will always create a new item frame as you can see in the first if check.

I think you’d be better off simplifying your inventory and not allowing there to be duplicate item entries in the player’s save data since you already have the “Amount” variable taking care of that.

print("time to load inventory")
for _,item in pairs(data.Inventory) do
	print("iterating through data")
	if item.Amount > 0 then
		print("found item")
		itemPanel = folderTemplate.item:Clone()
		itemPanel.Parent = playerGui:WaitForChild("ScreenGui"):WaitForChild("inventory"):WaitForChild("itemList")
		itemPanel.Name = item.Name
		itemPanel.itemname.Text = item.Name
		itemPanel.itemamt.Text = item.Amount
		itemPanel.itemimg.Image = item.Image
		itemPanel.Visible = true
	end
end

I’ll be real, I must’ve had some sleep-deprived reason for having that if statement be super specific, but it looks like you’ve solved it. Thank you so much!

(Oh, and I’ll edit to also mention that I cleared out the data from myself to clear out any duplicate items being saved, just in case anyone else stumbles upon this)