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:

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!

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.

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
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