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

I am not understanding the debounce function?

Asked by 9 years ago

Please explain and provide and example script such as one to prevent you from stepping on the same brick to many times. I have read the forum and do not understand the exact scripting property's of this function

0
Debounce is not some particular function. It's a general pattern of design that is useful when dealing with events. BlueTaslem 18071 — 9y
0
When a player touches a brick, the payer touches it about 10 times really really really fast. That means the function is called 10 times. A debounce lets have a cool down time. ConnorVIII 448 — 9y

1 answer

Log in to vote
2
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

Let's say you have a touched event that makes a given object to do something for a substantial amount of time (that is, not instantly) such as transparency fade to invisible, then back.

EXAMPLE

Object1 = script.Parent

function onTouched()
    for i = 1, 10 do
        Object1.Transparency = Object1.Transparency + .1
        wait()
    end
    for i = 1, 10 do
        Object1.Transparency = Object1.Transparency - .1
        wait()
    end
end

Object1.Touched:connect(onTouched)

Yet anytime a BasePart touches Object1, it will run the function, even if the function is already running.

So if you print the resulting Transparency of Object1, the Output will go something like this:

.1

.2

.3

.4 -- What if you touched Object1 at this point?

.1 -- The function starts over.

.2

.3

In most cases, you would want to avoid that. And to do that, you will use a debounce.

-- Static Variables
debounce = false        -- You can name 'debounce' as anything, because it is a boolean value. The CONCEPT itself is called 'debouncing'.

Object1 = script.Parent
--

function onTouched()
    if debounce == false then           -- What I just created here is essentially a 'lock' that ensures that the function is fired only ONCE.
        debounce = true             -- Now the 'if' statement can't run again until debounce is reverted back to 'false'.
        for i = 1, 10 do
            Object1.Transparency = Object1.Transparency + .1
            wait()
        end
        for i = 1, 10 do
            Object1.Transparency = Object1.Transparency - .1
            wait()
        end
        debounce = false
    end
end

Object1.Touched:connect(onTouched)

What the 'if' statement is basically saying when...

debounce is set to false:

if false == false then      -- The script will then run.

debounce is set to true:

if true == false then           -- The script will NOT run, therefore skipping over the 'if' statement.

EXAMPLE 2

If you want to have a part (Object2) to change it's color in a certain pattern every time it's touched, but want all three colors to appear each time.

debounce = false
Object2 = script.Parent

function Example2Function()
    if debounce == false then
        debounce = true                 -- You can say I've 'turned off' the 'if' statement until line 13
        Object2.BrickColor = BrickColor.new("Bright red")
        wait(.5)
        Object2.BrickColor = BrickColor.new("Bright green")
        wait(.25)
        Object2.BrickColor = BrickColor.new("Bright blue")
        wait(.25)
        debounce = false
    end
end

Object2.Touched:connect(Example2Function)

Regarding to your question about the for i = 1, 10, also known as the...

for Loop

It is a very convenient tool with a pretty substantial concept to wrap around.

A 'for' loop relays a given set of statements for a period amount of time, simply.

A 'for' loop's conditional statement consists of an initialized variable that equals to 2 - 3 parameters.

for [variable] = [initialnumber], [finalnumber], [step] do

initialnumber: The first loop the 'for' loop will run. The first parameter.

finalnumber: The last loop the 'for' loop will stop at. The second parameter.

step: The increment that the loop will go by. (E.g. if it's 2, then the loop will go by 2.) The third parameter (optional; if not mentioned, then the loop assume the step is '1').


The variable doesn't have to be strictly i. It is, of course, a regular variable and can be named as almost whatever you want.

'for' loops are instantaneous, which is why people add a 'wait()' function to pause the thread enough to view the intended effects.

EXAMPLE

for Tobu = 0, 500, 2 do         -- 'Tobu' is the initialized variable with '0' as the starting number and '500' as the ending number, while going up by '2'.
    print(Tobu)                 -- This will print the variable 'Tobu' in the output.
end

OUTPUT:

2

4

6

8

10

&c until it reaches 500.

For more information about the 'for' loop, visit here.

0
thank you for this information. I will review this and apply it to my script. I have one question regarding the first script you typed. what is the function of the "for i = 1, 10 do" in the script? I have never added this to a script function such as changing a property of an object using a script brooksdart 5 — 9y
0
what i mean in my question is "i" a know input i dont know or is it describing an object and also where is the value of 1,10 in this situation? in the object im manipulating? what is an example of these numbers and what they are for? brooksdart 5 — 9y
0
and does debounce refer to when an object is interacted with such as stepping on or clicking? ( or being activated in any way) brooksdart 5 — 9y
0
I've updated my answer that answers your questions about the 'for' loop. And debounce does not strictly have to be in functions, but it's ideal to do so, but of course it depends on the given circumstances. Redbullusa 1580 — 9y
View all comments (3 more)
0
I've also made a second example about debouncing below the first example. Redbullusa 1580 — 9y
0
ok well it looks like i have some homework to review :) thank you so much for the information!!!! brooksdart 5 — 9y
0
No problem! Don't forget to accept my answer if it helps a lot! :) Redbullusa 1580 — 9y
Ad

Answer this question