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

How do I add debounce to a script?

Asked by 6 years ago
Edited 6 years ago

When I asked my first question, it was answered but somebody else also suggested adding a debounce script to my restricted door. Could I get help with that? http://wiki.roblox.com/index.php?title=Debounce

01local door = script.Parent
02 
03function open()
04    -- This function will open the door.
05    door.CanCollide = false -- Make players able to walk through the door.
06 
07    for transparency = 0, 1, .1 do
08        door.Transparency = transparency
09        wait(.1)
10    end
11end
12 
13function close()
14    -- This function will close the door.
15    for transparency = 1, 0, -.1 do
View all 60 lines...
0
make sure search your question on the roblox developer wiki first before posting here, as it usually has the answer ;) cruizer_snowman 117 — 6y

4 answers

Log in to vote
0
Answered by
Decemus 141
6 years ago

Just add in a variable named something like local debounce = false and then set it to true when they touch it. Have it wait a second and then set it to false. Before you go and set it to true, though, have it check to see if it's false.

0
I'm new to this so I don't know where or how to add anything like that. TheRabster1428 0 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

To add a debounce, you can use a bool value to indicate whether the debounce is on or off. When it is on, simply don't open the door even if an allowed player touches it.

For example:

01local debounce = false
02 
03part.Touched:connect(function(touch)
04    if not debounce then
05        debounce = true
06 
07        touch:Destroy()
08    end
09 
10    wait(1)
11    debounce = false
12end)

The above example makes sure the script can only be run once a second at maximum.

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Adding a debounce i simple, Example;

1local debounce = true
2 
3script.Parent.Touched:Connect(function() -- change this if you want
4if debounce == true then -- checks if the debounce is true
5debounce = false --changes it to false
6-- put your script in here
7debounce = true --sets it to true after the script has run
8end

You can find more here: https://wiki.roblox.com/?title=Debounce

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

A debounce can actually be anything. It does not have to be a boolean. It can be a number or a string too.

HOW TO USE DEBOUNCE

You use debounce by first defining the variable outside of the given function:

1DebounceNumber = 1 -- Notice what I'm doing here

Then start your function, and when you start your function, add this:

1function ILoveRoblox()
2    if DebounceNumber == 1 then
3        DebounceNumber = 2 -- Again, doesn't have to be a number, it can be a boolean or string.

Followed by the rest of the script:

1        print("I love Roblox")
2        wait(3)
3        DebounceNumber = 1
4    end
5    if not DebounceNumber == 1 then break -- Debounce line
6end
7script.Parent.Touched:Connect(ILoveRoblox)

As I said before, a debounce can be anything (boolean, string, number). A debounce takes action when the value of the debounce variable is changed, and an if statement declares that "Hey, if this variable is equal to this value, start the function. If it is not equal to this value, stop the function immediately." That is the true way to use a debounce variable.

0
Why are you returning nothing at the end of a function? hiimgoodpack 2009 — 6y
0
What are you talking about? It returns the end value. DeceptiveCaster 3761 — 6y
0
hold on a sec DeceptiveCaster 3761 — 6y

Answer this question