GUIButton.Activated Firing multiple times

I am trying to get a remote event to fire one time when a gui button is pressed. Here is the code that I have currently:

local debounce = false

shop.btnSell.Activated:Connect(function()
			print("pressed")
			local ReplicatedStorage = game:GetService("ReplicatedStorage")
			local remote = ReplicatedStorage.Remotes.ChangeValue
			
			if debounce then
				debounce = false
				print("firing")
				remote:FireServer(metal, -amount)
				wait(1)
				debounce = true
			end

I have tried adding a debounce to make the event only allowed to fire one time in a second, but it didn’t work. The print() functions I have are printing 3 times whenever I press the button. The weird thing is with other buttons this doesn’t seem to be a problem. They still fire more than one time, but the code runs only once. For example:

shop.btnAdd.Activated:Connect(function()
			
			if amount < max then
				amount += 1
				shop.lblAmount.Text = amount
			end

This only adds one to the gui label, but the event in the first code block fires multiple times. I am very confused, does anyone know how I can fix this?

Is there a specific reason why you’re using .Activated on a Button? If not, then use .MouseButton1Click

Does MouseButton1Click work the same way as Activated?

MouseButton1Click fires once when the button is clicked, so I guess. Give it a try.

I tried it, it still fires the event multiple times.

btnSell is a TextButton, right?

Yes it is, is there something else I should use instead?

I don’t think there’s a problem with the script. Are you sure there are no duplicates of that script or code in other scripts that do the same thing?

I know there aren’t. The print statements that I’m getting are all coming from the same script.

I’m not really sure what to think of. I really don’t see anything wrong with the script or anything outside it. I don’t think I can help, sorry…

1 Like

I put the debounce int the script for the remote, and it works, but I can’t use the remote event several times.

You should be using MouseButton1Click and the debouncing checks/switching needs to be performed immediately inside the callback function connected to the event.