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

How does wait() and debounce work different from each other?

Asked by 3 years ago

I know that wait() delays things and debounce is cooldown but can't wait() just defeat the purpose of using a debounce?

1 answer

Log in to vote
0
Answered by
LeHelary 142
3 years ago

Uh correct me if I'm wrong, but as far as I know "debounce" is not an actual keyword. A debounce system is something used to prevent a block of code from being executed multiple times, causing problems. On the other hand, "wait" is a default Roblox function, that delays code execution. The "wait" function is also used for debouncing. Here's an example of wait() being used:

local block = workspace.Part -- a random part

block.Color = Color3.fromRGB(255,0,0) -- sets part color to red
wait(1) --> WAITS 1 SECOND BEFORE PROCEEDING
block.Color = Color3.fromRGB(0,255,0) -- sets part color to green

And here's an example of debouncing:

local block = workspace.Part -- a random part
local clickDetector = block.ClickDetector -- a random click detector
local isActive = false -- this is your boolean variable used for the debounce system

clickDetector.MouseClick:Connect(function() -- connects to the click detector event
    if isActive == false then -- checks if the process is still active, if it is it will not do anything
        isActive == true -- now the process is active, so the variable is set to true, this way the code won't be executed again until we set it back to false
        block.Color = Color3.fromRGB(255,0,0) -- sets part color to red
        wait(1) --> WAITS 1 SECOND BEFORE PROCEEDING
        block.Color = Color3.fromRGB(0,255,0) -- sets part color to green
        isActive = false -- now the process can be executed again when the event is fired
    end
end)

These scripts may have errors but it's just meant to explain those two things. Wait and debounce are two COMPLETELY different things. Hope this was clear enough.

0
click on "view source" if the code is hard to read in this format LeHelary 142 — 3y
Ad

Answer this question