GUI Button MouseButton1Click Firing Multiple Times

I’m making a dinosaur game, similar to dinosaur simulator, but the menu system isn’t functioning correctly.

Every time the player hits play and then goes back to the menu, the buttons in the menu fire one more time than before. For example, if I play and go back to menu 3 times, and then go to buy a dino, It’ll register the button press 4 times and charge me 4x the amount and give me 4 of the dino.

I’ve tried adding debounce, but to no avail. the script seems to be running them all at the same time.
I don’t know what could be causing the problem because as far as I can tell the GUI isn’t being cloned or anything. It all just looks completely normal.

If anyone has come across this problem before and/or knows potential solutions, please tell me.

You are creating multiple connections thats why, it’s something with your code, could you send that?

Here’s my code, is it because they’re in the remoteEvent?


repstorage.PlayerDied.OnClientEvent:Connect(function()
	wait()

	controls:Disable()
	ui.Enabled = true
	
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = menucamera:WaitForChild("Anchor").CFrame


	local selected = nil

	local function UpdateInfo(button)
		if selected ~= nil then
			local info = choosedinosaur.Info
			info.Visible = true
			info:FindFirstChild("Name").Text = button.Name
			info.Description.Text = button.Data.Description.Value
			info.Icon.Image = "http://www.roblox.com/asset/?id="..button.Data.IconID.Value
			info.Icon.UIAspectRatioConstraint.AspectRatio = button.Data.AspectRatio.Value
				
			if player.Owned:FindFirstChild(button.Name) == nil then
				info.TextButton.Text = "Buy for "..button.Data.Cost.Value.." Amber"
				info.TextButton.Selected = false
			elseif player.Dinosaur.Value == button.Name then
				info.TextButton.Text = "Selected"
				info.TextButton.Selected = true
			else
				info.TextButton.Text = "Select"
				info.TextButton.Selected = false
			end
		end
	end


	mainbuttons:FindFirstChild("Choose Dinosaur").MouseButton1Click:Connect(function()
		choosedinosaur.Info.Visible = false
		choosedinosaur.Visible = true
		mainframe.Visible = false
	end)

	mainbuttons:FindFirstChild("Settings").MouseButton1Click:Connect(function()
		settingsmenu.Visible = true
		mainframe.Visible = false
	end)

	choosedinosaur.Back.MouseButton1Click:Connect(function()
		choosedinosaur.Visible = false
		mainframe.Visible = true
	end)

	settingsmenu.Back.MouseButton1Click:Connect(function()
		settingsmenu.Visible = false
		mainframe.Visible = true
	end)


	mainbuttons:FindFirstChild("Play").Active = false

	mainbuttons:FindFirstChild("Play").MouseButton1Click:Connect(function()
		if player:WaitForChild("Dinosaur").Value ~= "" then
			game.ReplicatedStorage.LoadChar:FireServer()
			ui.Enabled = false
			player.Playing.Value = true
			repstorage.TargetChar.OnClientEvent:Connect(function(ncharacter)
				camera.CameraType = Enum.CameraType.Custom
				camera.CameraSubject = ncharacter:WaitForChild("HumanoidRootPart")
				
				controls:Enable()
			end)
		end
	end)

	choosedinosaur.Info.TextButton.MouseButton1Click:Connect(function()
			if player.Owned:FindFirstChild(selected.Name) == nil and player.Amber.Value >= selected.Data.Cost.Value then
				game.ReplicatedStorage.SetStat:FireServer("Amber", selected.Data.Cost.Value*-1)
				game.ReplicatedStorage.OwnDino:FireServer(selected.Name)
			elseif player.Dinosaur.Value ~= selected.Name and player.Owned:FindFirstChild(selected.Name) then
				game.ReplicatedStorage.SetDino:FireServer(selected.Name)
				mainbuttons:FindFirstChild("Play").Active = true
			end
	end)

	local dinolist = choosedinosaur.List:GetChildren()

	for i=1, #dinolist do
		if dinolist[i].ClassName == "Frame" and dinolist[i].Name ~= "ItemTemplate" then
			dinolist[i].Icon.MouseButton1Click:Connect(function()
				selected = choosedinosaur.List:FindFirstChild(dinolist[i].Name)
				
			end)
		end
	end

	while true do
		wait()
		UpdateInfo(selected)
	end

end)

Plus don’t use .MouseButton1Click use the .Activated event. Its way better to use this since it works across all devices.

every time PlayerDied.OnClientEvent fires, you re-run all those :Connect() calls, stacking listeners, move your :Connect() calls out of the remote event handler, run them once on script load or GUI creation, then in OnClientEvent just toggle visibility or reset state, or store each connection in a variable and call conn:Disconnect() before reconnecting.

1 Like

Glad you got a solution i’d recommend to stop using wait(x) and move to task.wait(x). It is apart of the task library it is a lot better to use and its a simple fix.