Help with inventory system

Hey developers! I am working on an inventory system and I have a part in a script which gets called when a player’s inventory gets updated (a new item gets added to the table, or something like that)
And pretty much I have a function which goes through items in the gui (and their buttons) and when the button is clicked, ill do some code to drop the item (not what i am looking for)

Now my problem is, when I click on an item, and the event has been called 5 times (the inventory has been updated 5 times) the print function runs 5 times. How do I make the list of items update when a new item is added but also make it get called once when a button is clicked?

ReplicatedStorage.UpdateInventory.OnClientEvent:Connect(function()
	local inventory = ReplicatedStorage.GetInventory:InvokeServer()
	if inventory then
		for i, item in pairs(inventory) do
			print("Name: "..item.Name..", Amount: "..item.Amount)
			if not InventoryGui.ItemList:FindFirstChild(item.Name) and item.Amount > 0 then	
				
				local itemFrame = script.Template:Clone()
				itemFrame.Name = item.Name
				itemFrame.ItemAmount.Text = item.Amount
				itemFrame.ItemName.Text = item.Name
				itemFrame.Parent = InventoryGui.ItemList
			else
				local itemFrame = InventoryGui.ItemList:FindFirstChild(item.Name)
				if itemFrame then
					if item.Amount > 0 then
						itemFrame.ItemAmount.Text = item.Amount
						print("updated amount for "..item.Name)	
					else
						itemFrame:Destroy()
					end	
				end						
			end
		end
	end
	
	for i, itemFrame in pairs(InventoryGui.ItemList:GetChildren()) do
		if itemFrame:IsA("Frame") then
			itemFrame.ItemButton.MouseButton2Up:Connect(function()
				print("drop item")
			end)
		end
	end
end)

this part is when the drop item thing should print once, but it does multiple times

	for i, itemFrame in pairs(InventoryGui.ItemList:GetChildren()) do
		if itemFrame:IsA("Frame") then
			itemFrame.ItemButton.MouseButton2Up:Connect(function()
				print("drop item")
			end)
		end
	end

Any help is appreciated!

From what I can deduct from the code you show and the problem you describe, it is most likely due to you continuously keep adding additional MouseButton2Up-event handlers to itemFrame.ItemButtons you already have added to previously, when that ReplicatedStorage.UpdateInventory.OnClientEvent-anonymous function is called.

I suggest you delete this code from the function:

	for i, itemFrame in pairs(InventoryGui.ItemList:GetChildren()) do
		if itemFrame:IsA("Frame") then
			itemFrame.ItemButton.MouseButton2Up:Connect(function()
				print("drop item")
			end)
		end
	end

And then make sure you only create a MouseButton2Up-event handler once, which can be done where you actually create (clone) a new itemFrame:

			if not InventoryGui.ItemList:FindFirstChild(item.Name) and item.Amount > 0 then	
				local itemFrame = script.Template:Clone()
				-- ...

				-- As we just created a new `itemFrame`, then also add
				-- a `MouseButton2Up`-event handler for it.
				itemFrame.ItemButton.MouseButton2Up:Connect(function()
					print("drop item")
				end)
			else
1 Like