Easy Written By SplitSecondFail153

Debounce Created on: 01-01-2019

What is a Debounce script?

What is a Debounce script?

A Debounce script prevents a script from being repeated before it can finish.

How can this help me?

Let's say you have a button on the floor, and you need it to print something into the console or complete an action when someone steps on it. This is how it would look normally.

game.Workspace.Button.Touched:Connect(function(hit)
	print ("Button Pressed!")

wait(5)
	print ("This action has been completed!")
end)

Although this seems like it would work, this is what would be outputted.

Button Pressed!
Button Pressed!
Button Pressed!
Button Pressed!
Button Pressed!
This action has been completed!
This action has been completed!
This action has been completed!
This action has been completed!
This action has been completed!

How do I fix this?

Fixing this is pretty simple, all it takes is adding a couple more lines to the script.

local buttonPressed = false
--Saves whether or not the button is pressed

Workspace.Button.Touched:Connect(function(hit)
	if not buttonPressed then
	--Is it not pressed?

		buttonPressed = true
		--Marks it as pressed, so that nothing else will execute

		print ("Button Pressed!")
		wait(5)
		print ("This action has been completed!")


	

	buttonPressed = false

	--Mark it as not pressed, so the script can execute again
		end
end)

You can also add a wait command at the end so the script will wait before it can be re-activated.

CREDITS

This little tutorial was paraphrased by me so it could be added into this game, here's a link.

https://developer.roblox.com/articles/Debounce

Go check it out if you want a more in depth tutorial on a debounce script!

Easy Written By SplitSecondFail153

See SplitSecondFail153's profile on Roblox

Discussion