Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is "Debounce" Used for?

I have no Idea what it is used for I know, you have to use this line of code.

debounce= true

and or

debounce = false

1 answer

Usually when you trigger something such as a Touched event, it will fire rapidly until the player stops touching the button. We usually don't want this. The technique used to prevent this is called debounce.

script.Parent.Touched:connect(function(hit)
	print("Touched!")
end)

The above code will rapidly spam the output with "Touched!" as long as a player's touching the script's parent. What if we only want it to trigger once?

debounce = false
script.Parent.Touched:connect(function(hit)
	if debounce == false then
		debounce = true
		print("Touched!")
	end
end)

The above will only print "Touched!" once, because debounce needs to be false for it to trigger. When it triggers, it sets debounce to true, and makes it impossible for the event to fire a second time.

The variable doesn't have to be named 'debounce' (it can be named whatever you want), but using 'debounce' or something similar makes it easier for others reading your code to figure out what the variable is used for.

You can also add a timer at the end to reset debounce to false, or reset it in a whole other function. The rest is really up to you.

2 comments

0

You failed at this, you need to make debounce false again after it's done. Or else it won't run a 2nd time

0

I explained that. Read the last portion.

This discussion is preserved as it was. New answers, comments, and votes are closed.