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!
01 | local boolean = false |
02 |
03 | function onTouched() |
04 | if boolean then |
05 | boolean = false |
06 | --code |
07 | wait( 4 ) |
08 | else |
09 | boolean = true |
10 | --code |
11 | wait( 4 ) |
12 | end |
13 | end |
14 |
15 |
16 | script.Parent.Touched:connect(onTouched) |
01 | local boolean = true |
02 | local action = 1 |
03 |
04 | function 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 ) |
Something like this should work.
01 | local boolean = false |
02 |
03 | function onTouched() |
04 | if not boolean then |
05 | boolean = true |
06 | wait( 4 ) |
07 | --run your code |
08 | boolean = false |
09 | end |
10 |
11 |
12 | script.Parent.Touched:connect(onTouched) |
Here's a practical use for debounce.
01 | local debounce = false |
02 | script.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 |
11 | end ) |