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

How exactly does a debounce work? Can someone explain it to me?

Asked by 8 years ago
Edited 8 years ago

I do understand why a debounce is required (because all event handlers execute at the same time) but what makes it work? Why does inserting a debounce = true or debounce = false make the script work? I am not totally confident in debounces and I need some help.

3 answers

Log in to vote
1
Answered by 8 years ago

Debounces are not really required in a script. They are just meant for a code not to be run so many times that it "crashes". For instance lets say I make a part that when it is clicked it turns the time of day to a random setting. Without a debounce the player would be able to click it randomly and cause a crash, error, or malfunction in the script. A debounce can be named anything you want ranging from just f = true or door = false. I see you are confused on the Falseand True. This is just a Boolean. That is really all the debounceis. For example:

1click = true
2script.Parent.ClickDetector.MouseClick:connect(function()
3     if click == true then
4               click = false -- prevents the spam
5          print("It is true!!!")
6          wait(1)
7                click = true
8           end
9end)

This could would work with true or false in the mix. It doesn't matter which part of the Boolean you use. If you have any more questions feel free to comment in my answer. If this helped you then don't forget to accept. Thanks for asking the question!

0
Ooh thank you! But does it lock it because making a click variable true will enable the function and false will disable it to work or something else? crystalclay 70 — 8y
1
@crystalclay It can be either true or false, the true or false (in this case) doesnt mean anything. It just checks if it is true or false. And if so it will run the code, if not it will wait for it to be true ( if every it becomes true or false again). yougottols1 420 — 8y
0
Gotcha! Thank you crystalclay 70 — 8y
Ad
Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

A debounce is to stop code from running 3929948382848288284 times at the same time. Like when to use this is for functions and stuff.

1function Stuff()
2--stuff
3end

But when it runs, it could run the stuff mutiple times at the same time! So we gotta do this.

1local Debounce = false --Make the debounce name here, just change the name where other names are debounce.
2function Stuff()
3if Debounce == false then --Checks if the value is false, else if it true then it WILL NOT run.
4local Debounce = true --Making the rest of the code running again not be able to go past the if statement
5--stuff
6else
7warn("STOP MAKING THIS SCRIPT RUN!!")
8end
9end

Like a door, once it has a debounce value of true the door is locked! And when the debounce value is false, it is not locked! Or you can do this, checking if some stuff is or has this. And you cant go through a locked door, neither can the script.

01if script.Parent:FindFirstChild("LocalScript") then
02local Check1 = false
03else
04local Check1 = true
05end
06 
07if not game.Workspace:FindFirstChild("Part") then
08local Check2 = false
09else
10local Check2 = true
11end
12 
13function Clicked()
14if Check1 == false and Check2 == false then
15--stuff
16else
17print("Y U MAKING ME RUN FOR NO REASON")
18end
19end
Log in to vote
1
Answered by 8 years ago

Debounce

A debounce system is a set of code that keeps a function from running too many times. It comes from the idea of mechanical switch bounce, where a switch bounces when pushed, creating multiple signals. In the context of ROBLOX, this problem occurs mainly with the Touched event, when a part touches another multiple times in a short space of time, but may be useful in other cases as well.

found on...

http://wiki.roblox.com/index.php?title=Debounce

0
I've already read the wiki, but it did not explain what "true" and "false" mean. Or what they are for. crystalclay 70 — 8y
0
The true/false is just a bool and helps determines if it should run at a certain time or do something when the value is changed @crystalclay DeveloperSolo 370 — 8y

Answer this question