2 questions about Debounce

I’m struggling to learn about debounce. My two main questions are when you create a debounce variable, why is it set to false at the beginning then set to true after the “If statement”? And what does false mean and true when debounce is set to it? Any help is appreciated!!

1 Like
local variable=true
if variable then--true, the if statement has been passed, it will run
print(1)
end

→ 1

local variable=false
if variable then--false, the if statement has not been passed, it will not run
print(2)
end

→ (nothing)

Debounce can be used to stop a function (often connected to a touch event) from being run more than once.

Take a lava block for example. Debounce is initially set to false as no one has touched the block.
If I touch the block, we will run a kill function and then set debounce to true.
Then after a few seconds have passed, we will set debounce to false.

This means that if another part of the player touches the block very soon after the first part, it will see that debounce has been set to true (very recently) - meaning the kill function has already run and will therefore not run it again.

1 Like
local Deb=false
local function YouCannotFloodThisFunction()
  if Deb then return end
  Deb=true
  print(1)
  wait(5)
  Deb=false
  return "success"
end

if an event calls this function, it will only run if the variable is false, it will be set to true which creates a 5 seconds wait as the variable changes to false after the 5 second wait

here is a script.

local touch = script.Parent.ClickDetector

touch.Clicked:Connect(function()
    print("Hi!")
end)

This is the output:

> "Hi!"(x53)

Here is the thing. I only want it to say “hi” once. It is saying it 53 times though. This is what you use debounces for.

The correct usage of the script:

local touch = script.Parent.ClickDetector
local isClicking = false --Also known as a Debounce

touch.Clicked:Connect(function()
    if isClicking == false then
        isClicking = true
        print("Hi!")
        wait()
        isClicking = false
    end
end)

Here is the output:

> "Hi!"

Notice how it only says “hi” once.

Debounce is just used to keep players from spamming something or doing something more than once. So lets take a simulator game as an example. You don’t want players to just spam click a button to give them tons of money, you want to add a debounce to help keep it so they can only click the button once every so often. Debounce is more of just a variable you define as true or false. You can use it like a BooleanValue to help keep your script/function from doing certain things or even breaking itself.

You can choose whether to set the variable to true or false, it doesn’t really matter. It matters how you use it in your code. So lets say you choose to make it true in the beggining, what you would do is:

local debounce = true
local function SomeFunction()
    if debounce == true then -- checks to see if the debounce is set to true
        debounce = false -- sets it to false to stop the function from flooding
        print("debounce")
        wait(3)
        debounce = true -- sets debounce back
    end
end

Basically what I have done here is made a function that prints and uses debounce to help stop the function from flooding. So if this were used in a simulator to make money, lets say someone was pressing a button, it would wait 3 seconds before setting debounce back so the player could make money again.

This is pretty much adding off to what Naco88 had said as well.

So debs are used to prevent players from doing unwanted activities, there are 2 types of them. Array and Standard debs.

Array debs go some thing like this:

local T ={}

T[“plrName”] = false 

If not T[“plrName”] then
T[“plrName”] = true
wait()
T[“plrName”] = false
end 

Then there is standard debouncing that goes something like this:

Local A = false

If not A then
A = true
wait()
A = false
end

Now Array debs are used on sever to keep track of remote events and functions and to prevent unwanted activity.

Standard debs are used on client to prevent unwanted activity.

It is good to use both kinds of them to keep track of player data and to prevent exploits. As client denounces can be exploited. Sever cannot be touched by exploits and that’s why array debs are useful.

Now keep in mind true and false is just a way of saying is and isn’t. For instance A = false, or not A. It’s saying A isn’t true, or another example ; A = true, this is like saying Is A or A.

So when you see a conditional you can see them like this;

if A then or if not A then

Now you may be asking, why can’t I use standard debs for sever. Well the answer is because if you set a value to false on sever it changes for everyone. However if you make a player in a list and change there value it will only change for them. So remember don’t use standard debs on sever unless it is intended to be sever wide, as you will run into user errors.

Now why debs are set to false is dependent on the scripter. You can make the debs true or false. However you will need to keep that in mind when doing conditional statements. As you would run into user errors if it is not in mind.

In summery debouncs are essential to bring a scripter, whiteout them we can’t script these massive games that we see today.