Apparently it stops the code from repeating it's self. How do I use it please?
How does debounce work?
2 answers
All scripts on the server are shared with all of the clients/players. The idea is to stop any unwanted input whilst the code is processing.
An good example is the touched event, which can be triggered many times by one or more objects.
local debounce = true --this needs to be outside of the event otherwise it will be overwritten when the event is ran which is what we want to avoid.
[random object].Touched:connect(function (part)
if debounce then --debounce is true so the code will run
debounce = false --code is in use
--your processing ect
debounce = true --code finished allow another even
end
end)
This will only enable one event to be processed at any one time and stops any overflow of events.
A Debounce is a method in the .lua language that prevents a Function from firing multiple times on execution, or as kingdom5 had explained, to prevent another from firing while code is being processed. This is primarily used for Functions that use the Touched event, and other functions, as sometimes I use it for GUIs. This is an example of a function without a Debounce:
function onTouch() --Here is out function
print("I printed first! :D") --This will print into the Output when the function is called
wait(2) --Waits two seconds before going onto the next code
print("I printed second! :D") --Prints into the Output two seconds after the first
end --Ends the chunk
Part.Touched:connect(onTouch) --Connects the Event 'Touched' to a 'BasePart' Instance
And as a result, the Output would look something like this:
--[[
I printed first! :D
I printed first! :D
I printed first! :D
I printed first! :D
I printed first! :D
I printed first! :D
I printed first! :D
I printed first! :D
I printed second! :D
I printed second! :D
I printed second! :D
I printed second! :D
I printed second! :D
I printed second! :D
I printed second! :D
I printed second! :D
--]]
Now, a function with a Debounce:
local Debounce = true --Here is our Debounce; It'll prevent the function from firing multiple times
function onTouch() --Here is our function
if Debounce then --The 'if' statement is checking if 'Debounce' is true; The 'if' statement checks if the Condition is equal to it's Argument
Debounce = false --If so, it'll set 'Debounce' to 'false', starting the 'Debounce'
print("I fired! :D") --This will print into the 'Output' first
wait(2) --Waits two seconds
print("I fired last! :D") --Prints afterwards
Debounce = true --Sets 'Debounce' back to 'true', allowing the function to be used again
end --Ends the chunk to the 'if' statement
end --Ends the chunk
Part.Touched:connect(onTouch)
And, as a result, the Output will look like this:
--[[
I fired! :D
I fired last! :D
I fired! :D
I fired last! :D
I fired! :D
I fired last! :D
I fired! :D
I fired last! :D
--]]
Information left out:
- The
Ifstatement is used to check if aConditionis equal to it'sArgument, or if the Condition istrue.
Hope this helped!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Alright thanks!
The event is still being processed, but the code inside the conditional won't be processed.