How to debounce(Explanation Please)

I want to create a debounce system so when a player goes and makes contact with the shop they can only see the shopGUI pop up every once in a specific amount of time.

I tried to create my own but I feel that I don’t know enough about it to create my own… so I’m looking for someone to give me a thorough explanation of how it works so it’ll make sense to me.

I tried looking on youtube and found a couple videos that showed how to debounce but I feel that they didn’t explain it too well… and their code didn’t work(I have tried AlvinBlox and others). I am not asking you to write me a whole debounce system so please don’t do that. Just explain to me what debouncing is thoroughly and I’ll see if I can create my own debouncing system.

Thank you!

6 Likes

This DevHub article might be able to help you: Debounce Patterns | Documentation - Roblox Creator Hub

A lot of the examples are for the Touched event too, so hopefully that’ll help you to understand better.

1 Like

Alright so basically "Debounce"is just a variable that you will set to true/false depending if you can run the function again. For example.

local MyVariable = false

part.Touched:Connect(function()
   if MyVariable == false then --  Making sure the value is set to false.. Aka the function can run
   MyVariable = true -- If the function can run, then we're going to set that value to true so it won't get past that if statement
   --Code 
   wait(TimeToWait) -- Wait however long until you want to start again
   MyVariable = false -- And finally, we're going to set this value back to false so the function can start again. 
  end
end)
6 Likes

Ok, thanks I’m checking it out right now. Would you mind if I ask you any questions on it that I have?

1 Like

I tried this but it didn’t work… should I put the debounce script in the original local script for the shopGUI or should I put it in another local script(Or does it not matter and that method of debouncing isn’t what I’m looking for?)

Short answer. It’s just a way of preventing 2 different sections of code from running at once. Say you wanted to animate a door open, you’d make a variable like local doorOpening = false, set it to true before you start animating the door open, and then set it to false after the door closes so it is set to true just before the animation begins (obviously you don’t want it to begin after) and set to false right after the animation ends.

Ideally, you’d have another variable like isDoorOpen = false and when maybe the door is clicked you run an if statement such as if isDoorOpen == false then -- open door, but then within that if statement you’d check if door is currently in the process of closing so you don’t run the opening and closing animation at the same time.

7 Likes

Most likely it didn’t run because I used part.Touched which is just an example… Place the script inside of a part and change it to script.Parent.Touched… Then just replace --Code with a simple print statement. And of course the time to wait

Thanks!! I think I’ve got it figured out now.

Just wanted to add that adding a debounce to a piece of code that doesn’t yield won’t do anything. Roblox Lua is single-threaded (at least this is the behavior we see), so there are no race conditions if your code doesn’t yield. Knowing what yields and what doesn’t is important when using debounces.

Lua itself is single-threaded and RLua is a derivation of vanilla Lua. There was mention about multi-threaded Lua at RDC but I have no clue how that’ll go.

1 Like

What do you mean by “single-threaded”?

My opinion, debounces is just the name.
It gets more confusing when they actually name the debounce variable, “debounce.”

That’s because when you actually look through code with a debounce and the variable name is called “debounce” you have to trace the code to see what it’s used for. Personally, I name my debounces very particular so that I’m able to understand what it does. This allows for multipled debounces and better debugging in the future.

What most people do:

local debounce = true
button.MouseButton1Click:Connect(function()
    debounce = not debounce
    if debounce then
        print('Closing..')
    else
        print('Opening..')
    end
end)

What I recommend doing:

local isShopOpened = true -- actually telling us the current state of something instead of saying "debounce is true"
button.MouseButton1Click:Connect(function()
    isShopOpened = not isShopOpened 
    if isShopOpened then
        print('Closing..')
    else
        print('Opening..')
    end
end)
1 Like

Single-threaded means the code is executed line by line, with no possibility of other code executing at the same time. The only time you can transfer control to another code is when you yield the current thread (by means of wait(), for example), or when there is no more code to be executed.

That’s not what a debounce is nor is that the common use of a debounce.

Debounces are primitive state switches like this, yes, but they’re a variable used to prevent something from running over an n time period, not as a variable to switch between a true and false state. That’s just a normal boolean operation.

See: