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.
Debounce
s 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 False
and True
. This is just a Boolean. That is really all the debounce
is. For example:
1 | click = true |
2 | script.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 |
9 | end ) |
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!
A debounce is to stop code from running 3929948382848288284 times at the same time. Like when to use this is for functions and stuff.
1 | function Stuff() |
2 | --stuff |
3 | end |
But when it runs, it could run the stuff mutiple times at the same time! So we gotta do this.
1 | local Debounce = false --Make the debounce name here, just change the name where other names are debounce. |
2 | function Stuff() |
3 | if Debounce = = false then --Checks if the value is false, else if it true then it WILL NOT run. |
4 | local Debounce = true --Making the rest of the code running again not be able to go past the if statement |
5 | --stuff |
6 | else |
7 | warn( "STOP MAKING THIS SCRIPT RUN!!" ) |
8 | end |
9 | end |
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.
01 | if script.Parent:FindFirstChild( "LocalScript" ) then |
02 | local Check 1 = false |
03 | else |
04 | local Check 1 = true |
05 | end |
06 |
07 | if not game.Workspace:FindFirstChild( "Part" ) then |
08 | local Check 2 = false |
09 | else |
10 | local Check 2 = true |
11 | end |
12 |
13 | function Clicked() |
14 | if Check 1 = = false and Check 2 = = false then |
15 | --stuff |
16 | else |
17 | print ( "Y U MAKING ME RUN FOR NO REASON" ) |
18 | end |
19 | end |
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...