GuiButton.MouseButton1Click running multiple times after 1 click, depending on previous factors

Recently I have been having some issues with my shop. Whenever I click buy on an object, it is influenced by previous item selections.
As you can see in the image below, whenever I select an item to buy it prints “not robux boombox”. When I then click buy on an object, the function runs depending on how many images selected, this results in buying the item X times.
image

This is a local script I’m using for the shop:

Shop script
for i, item in pairs(itemsFolder:GetChildren()) do
	
	local realName = item.Name
	local name = item.itemName.Value
	local price = item.Price.Value
	local desc = item.Description.Value
	local image = item.itemImage
	
	local itemSelection = script.ItemSelection:Clone()
	
	image.Parent = itemSelection
	
	itemSelection.Parent = itemScroller
	
	itemSelection.LayoutOrder = price
	
	UIGridLayout.SortOrder = Enum.SortOrder.LayoutOrder
	UIGridLayout:ApplyLayout()
	
	itemSelection.MouseButton1Click:Connect(function()
		
		
		if realName ~= "cartoonyRainbowBoombox" and realName ~= "backpackBoombox" then
			print("not robux boombox")
			GuiSound:Play()
			itemPreview.ItemName.Text = name
			itemPreview.ItemDescription.Text = desc
			itemPreview.RealName.Value = realName


			if backgroundFrame:FindFirstChild('itemImage') then
				backgroundFrame:FindFirstChild('itemImage'):Destroy() 
			end


			local image2 = image:Clone()
			image2.Parent = backgroundFrame
			itemPreview.Visible = true


			local boomboxFound = false
			for i, boombox in pairs(boomboxes:GetChildren()) do
				if realName == boombox.Name then
					boomboxFound = true
					break
				else
					boomboxFound = false
				end			
			end

			if boomboxFound == true then
				if realName == characterEquipped:FindFirstChildWhichIsA("Part").Name then
					itemPreview.robuxBuyButton.Visible = false
					itemPreview.BuyButton.Visible = false
					itemPreview.EquippedButton.Visible = true
					itemPreview.EquipButton.Visible = false
				else
					itemPreview.robuxBuyButton.Visible = false
					itemPreview.BuyButton.Visible = false
					itemPreview.EquippedButton.Visible = false
					itemPreview.EquipButton.Visible = true
				end
			else
				itemPreview.robuxBuyButton.Visible = false
				itemPreview.EquippedButton.Visible = false
				itemPreview.EquipButton.Visible = false
				itemPreview.BuyButton.Visible = true
				itemPreview.BuyButton.textLabel.Text = AbbreviateNumber:Abbreviate(price)
			end
			
			
			
			
		else
			GuiSound:Play()
			
			local robuxPrice = item.RobuxPrice.Value
			itemPreview.ItemName.Text = name
			itemPreview.ItemDescription.Text = desc
			itemPreview.RealName.Value = realName
			if backgroundFrame:FindFirstChild('itemImage') then
				backgroundFrame:FindFirstChild('itemImage'):Destroy() 
			end
			local image2 = image:Clone()
			image2.Parent = backgroundFrame
			
			
			local GamepassID = item.gamepassID.Value
			if MarketplaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
				if boomboxes.Equipped:FindFirstChild(realName)then
					itemPreview.robuxBuyButton.Visible = false
					itemPreview.BuyButton.Visible = false
					itemPreview.EquipButton.Visible = false
					itemPreview.EquippedButton.Visible = true
				else
					itemPreview.robuxBuyButton.Visible = false
					itemPreview.BuyButton.Visible = false
					itemPreview.EquipButton.Visible = true
					itemPreview.EquippedButton.Visible = false
				end
			else
				itemPreview.robuxBuyButton.Visible = true
				itemPreview.BuyButton.Visible = false
				itemPreview.EquipButton.Visible = false
				itemPreview.EquippedButton.Visible = false
				itemPreview.robuxBuyButton.textLabel.Text = robuxPrice.." Robux"
			end
			itemPreview.Visible = true
			
			itemPreview.robuxBuyButton.MouseButton1Click:Connect(function()
				print("robux buy button presed")
				MarketplaceService:PromptGamePassPurchase(player, GamepassID)
				MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, gamepassId, waspurchased)
					if waspurchased == true then
						OnEquipped:FireServer(itemsFolder[itemPreview.RealName.Value])
						itemPreview.robuxBuyButton.Visible = false
						itemPreview.BuyButton.Visible = false
						itemPreview.EquipButton.Visible = false
						itemPreview.EquippedButton.Visible = true
					end
				end)
			end)

		end
		
		itemPreview.BuyButton.MouseButton1Click:Connect(function()
			print("buy button pressed")
			local coins = leaderstats.Coins.Value
			if price > coins then
				badGuiSound:Play()
			else
				game.ReplicatedStorage.OnItemBought:FireServer(itemsFolder[itemPreview.RealName.Value])
				GuiSound:Play()
				itemPreview.robuxBuyButton.Visible = false
				itemPreview.BuyButton.Visible = false
				itemPreview.EquipButton.Visible = false
				itemPreview.EquippedButton.Visible = true
			end
			
		end)
	end)
end

I have tried many approaches to try and fix this but nothing seems to have worked yet. Any suggestions would be appreciated!

1 Like

your functions are nested, you can try disconnecting the connections inside of the first clicked function.

example:

local conn
local conn2
local conn3

conn = itemSelection.MouseButton1Click:Connect(function()
	if conn2 then
		conn2:Disconnect()
	end
	if conn3 then
		conn3:Disconnect()
	end
	
	--your codes
	
	conn2 = itemPreview.BuyButton.MouseButton1Click:Connect(function()
		-- your codes

		conn2:Disconnect()
	end)

	conn3 = itemPreview.robuxBuyButton.MouseButton1Click:Connect(function()
		-- your codes

		conn3:Disconnect()
	end)
end)
4 Likes