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

Help w/ debounce?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

I tried to make a debounce to prevent "Hello" from spamming but it does not work. I want to touch the part them it prints then waits 2 seconds before it could print again.

debounce = false
script.Parent.Touched:connect(function(hit)
    debounce = false
    print'Hello'
    debounce = false
    wait(2)
    debounce = true

end)

2 answers

Log in to vote
0
Answered by 9 years ago

Debounces

debounces aren't some hidden functionality that come along with functions. Merely stating debounce = false and debounce = true gives the compiler no information whatsoever about preventing the function from running at all.

The variable name debounce does not trigger any extra functionality. It is a variable just like all else. The whole point of a debounce is to set some form of regulation (a conditional in our case) that will tell the function to act or not act based on the current value of the debounce.

To clarify with code:

DigitalVeerLovesSteampunk = false; --To show that you can have any variable name
script.Parent.Touched:connect(function(hit)
if not DigitalVeerLovesSteampunk then
DigitalVeerLovesSteampunk = true;
print("Hello!")
wait(2)
DigitalVeerLovesSteampunk = false;
end
end)

As you can see, we basically check if the debounce (in our case it is DigitalVeerLovesSteampunk) is a certain value. If it is, run the function. If not, don't do anything. This is how debounces work. They aren't any special lua-designed functions. It's merely just a check-and-peck.

Ad
Log in to vote
2
Answered by
Vezious 310 Moderation Voter
9 years ago

You're using debounce wrong. Debounce can be replaced with any word and doesn't have to start off as false. You can replace debounce with CanTouch. DebounceJust Stops The script from running again until the it's finished its original cycle.

local CanTouch = true
script.Parent.Touched:connect(function(hit)
if CanTouch == true then 
CanTouch = false
print"Hello"
wait(2)
CanTouch = true

The first time we use it is to set up the variable. The Second Time We Use it is prevent it from activating twice. The third time is so you tell it that the script has finished it's cycle and can be activated again.

Answer this question