How to wait for events to complete themselves before activating again?

I just want to know if there is a way to make en event only capable of running once at a time. By that I mean that the event can not trigger again until it is done.

1 Like

You can use a ‘debounce’ variable.

local pressed = false

buttonPart.Touched:Connect(function()
	if (not pressed) then
		pressed = true
		print("brick was touched. it can't be activated again for 5 seconds.")
		buttonPart.BrickColor = BrickColor.Black()
		wait(5)
		pressed = false
		print("ready to activate.")
		buttonPart.BrickColor = BrickColor.White()
	else
		print("brick cannot be activated yet.")
	end
end)

And if you want an event to run only once and never again, you can disconnect it in the listener function:

local conn
conn = buttonPart.Touched:Connect(function()
	buttonPart.BrickColor = BrickColor.Black()
	print("brick was touched. this block of code will never run again because we are disconnecting the function afterwards.")
	conn:Disconnect()
end)
1 Like

Thank you! it worked perfectly.