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?
You should be using MouseButton1Click and the debouncing checks/switching needs to be performed immediately inside the callback function connected to the event.