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:
click = true script.Parent.ClickDetector.MouseClick:connect(function() if click == true then click = false -- prevents the spam print("It is true!!!") wait(1) click = true end 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.
function Stuff() --stuff end
But when it runs, it could run the stuff mutiple times at the same time! So we gotta do this.
local Debounce = false --Make the debounce name here, just change the name where other names are debounce. function Stuff() if Debounce == false then --Checks if the value is false, else if it true then it WILL NOT run. local Debounce = true --Making the rest of the code running again not be able to go past the if statement --stuff else warn("STOP MAKING THIS SCRIPT RUN!!") end 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.
if script.Parent:FindFirstChild("LocalScript") then local Check1 = false else local Check1 = true end if not game.Workspace:FindFirstChild("Part") then local Check2 = false else local Check2 = true end function Clicked() if Check1 == false and Check2 == false then --stuff else print("Y U MAKING ME RUN FOR NO REASON") end 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...