Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

What is "Debounce" Used for?

Asked by
IcyEvil 260 Moderation Voter
9 years ago

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

Log in to vote
0
Answered by
MrFlimsy 345 Moderation Voter
9 years ago

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.

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 lomo0987 250 — 9y
0
I explained that. Read the last portion. MrFlimsy 345 — 9y
Ad

Answer this question