Problem with objects that keep adding

Hello everyone,

I have the weirdest problem. I have been fighting with it for over a week now but I can’t figure it out. I’m probably looking over something, but hopefully you can help me.

For the purpose of a store, in ServerStorage I have a folder ‘Skins’ that currently holds 3 models. Once the game starts, these models are cloned into ReplicatedStorage so I can list them in a UI. That script works well, so no need to show it here. I added a configuration to each model to set some attributes and make it easier to copy those.

Now in a LocalScript I’m filling the storeUI by cloning a premade ‘Item’ template. The script looks like this:


ReplicatedStorage.storeItemsToClient.OnClientEvent:Connect(function(skinsTable)
	for i,v in pairs(skinsTable) do
		local currentitem = itemTemplate:Clone()
		currentitem.Name = v.Name
		currentitem.Parent = storeFrame
		currentitem.ItemName.Text = v.Configuration:GetAttribute("FriendlyName")
		currentitem.Image = "rbxassetid://" .. v.Configuration:GetAttribute("Thumb")
		currentitem.ItemPrice.Text = v.Configuration:GetAttribute("Cost")
		currentitem.MouseButton1Click:Connect(function()
			detailsFrame.itemName.Text = v.Configuration:GetAttribute("FriendlyName")
			detailsFrame.itemDescription.Text = v.Configuration:GetAttribute("Description")
			detailsFrame.itemPrice.Text = v.Configuration:GetAttribute("Cost")
			detailsFrame.itemImage.Image = "rbxassetid://" .. v.Configuration:GetAttribute("Thumb")
			detailsFrame.buyButton.MouseButton1Click:Connect(function()
				ReplicatedStorage.buyingItem:FireServer(v) 	-- A RemoteEvent that sends the instance to the server to deal with the sale.	
			end)
		end)
	end
end)

So when I click one of the three items on the left, if fills the details screen on the right. This also works without a problem.

Now the actual problem; If I click one of the models left multiple times, or multiple models, it keeps adding them. So when I click ‘Buy’ button it sends not the currently selected item, but everyone I have clicked. I’m sorry if my post is unclear, but it’s very hard to explain. But just to be clear once more, if I click item 1, 2 and 3 it sends them all. If I click them 15 times, it sends them 15 times to the RemoteEvent.

What am I not seeing here?

Hopefully I have been clear enough and thank you so much in advance!

This is because for every item, you connect a .MouseButton1Click RBXSignal to detailsFrame.buyButton. To fix this, consider connecting to it separately.

Here’s an example:

-- global which holds the currently selected item
local currentlySelected

-- connecting to the .buyButton separately
detailsFrame.buyButton.MouseButton1Click:Connect(function()
	if currentlySelected then
		ReplicatedStorage.buyingItem:FireServer(currentlySelected)
	end
end)

ReplicatedStorage.storeItemsToClient.OnClientEvent:Connect(function(skinsTable)
	for i,v in pairs(skinsTable) do
		local currentitem = itemTemplate:Clone()
		currentitem.Name = v.Name
		currentitem.Parent = storeFrame
		currentitem.ItemName.Text = v.Configuration:GetAttribute("FriendlyName")
		currentitem.Image = "rbxassetid://" .. v.Configuration:GetAttribute("Thumb")
		currentitem.ItemPrice.Text = v.Configuration:GetAttribute("Cost")
		currentitem.MouseButton1Click:Connect(function()
			-- set currentlySelected global to the skin/item
			currentlySelected = v

			detailsFrame.itemName.Text = v.Configuration:GetAttribute("FriendlyName")
			detailsFrame.itemDescription.Text = v.Configuration:GetAttribute("Description")
			detailsFrame.itemPrice.Text = v.Configuration:GetAttribute("Cost")
			detailsFrame.itemImage.Image = "rbxassetid://" .. v.Configuration:GetAttribute("Thumb")
		end)
	end
end)
2 Likes

Of course!! Now it makes perfect sense. Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.