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

What is the code for debounce and how does it work?

Asked by
QLike1 0
10 years ago

What is the code for debounce and how does it work?

1 answer

Log in to vote
0
Answered by 10 years ago

debounce works to prevent a script from doing the same thing repeatedly if you only want it to happen once.

for example if you want a function to execute when a character's Right leg touches a brick, the ROBLOX engine will usually interpret the contact multiple times, thus the function is also executed many times. Debouncing isn't that complicated and just sorta acts like a switch to prevent the function from re-executing while it's running.

Here's an example:

local Pressed = false -- this is a local variable

Workspace.Button.Touched:connect(function(hit)
    if not Pressed then 

        Pressed = true

        print("Touched")
        wait(1)
        YourFunction() -- executes whatever you want

      Pressed = false
    end
end)

Now during the 1 second wait that the "Pressed" variable is true, if the Button is touched again within that timeperiod, it will prevent the function from double-executing.

Hope this helped :)

Ad

Answer this question