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

How Do You Add Debounce In This Script?

Asked by 8 years ago

How do you add debounce in this script? I want it to wait 4 seconds before it does the second part of the coding when touched. So, it does the first action, then wait 4 seconds, next do the second action, and at last wait another 4 seconds before it does the first action again. Thanks!

01local boolean = false
02 
03function onTouched()
04    if boolean then
05        boolean = false
06--code
07wait(4)
08    else
09        boolean = true
10--code
11wait(4)
12    end
13end
14 
15 
16script.Parent.Touched:connect(onTouched)

2 answers

Log in to vote
0
Answered by 8 years ago
01local boolean = true
02local action = 1
03 
04function onTouched()
05    if boolean == true then
06        boolean = false
07        if action == 1 then
08            action = 2
09                --action1
10            wait(4)
11            boolean = true
12        elseif action == 2 then
13            action = 1
14                --action2
15            wait(4)
View all 21 lines...
0
Sorry but I need it to do the second action, I think this only does 1 action GatitosMansion 187 — 8y
0
What do you mean? NovaMagic 73 — 8y
0
Is that what you mean? NovaMagic 73 — 8y
0
Sorry that I am not clear xD I mean like when it is touched the first time, it does something. Then when it is touched the second time, it does a different action. GatitosMansion 187 — 8y
View all comments (3 more)
0
Okay, I think I got it this time NovaMagic 73 — 8y
0
Thanks it worked! I have two questions, but it is not necessary to answer them, you can just ignore them if you want. Is boolean still nessesary ? Also, is there an official word to replace "action"? GatitosMansion 187 — 8y
0
Boolean is the debounce, you can rename it to whatever you like, as for your second question, I don't know. Maybe scope? I'm still new to scripting myself. NovaMagic 73 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Something like this should work.

01local boolean = false
02 
03function onTouched()
04    if not boolean then
05    boolean = true
06    wait(4)
07    --run your code
08    boolean = false
09end
10 
11 
12script.Parent.Touched:connect(onTouched)

Here's a practical use for debounce.

01local debounce = false
02script.Parent.Touched:connect(function(hit)
03    if not debounce then
04        debounce = true
05        local hit = hit.Parent
06        local hum = hit:FindFirstChild("Humanoid")
07        hum.Health = hum.Health - 5
08        wait(3)
09        debounce = false
10    end
11end)
0
Sorry but I need it to do the second action, I think this only does 1 action. In my script there is 2 coding parts. I need it to do the first action, wait and then do the second action GatitosMansion 187 — 8y
0
Can you post your code? It's kind of hard to answer your request if we're not sure what you're doing. Almorpheus 10 — 8y

Answer this question