Hi, I have a script in a brick that's supposed to make a sound play when the brick is touched by the player. It looks like this:
01 | local SP = script.Parent |
02 |
03 | function onTouch(hit) |
04 | local H = hit.Parent:FindFirstChild( "Humanoid" ) |
05 | if H ~ = nil then |
06 | SP.S:Play() |
07 | end |
08 | end |
09 |
10 | script.Parent.Touched:connect(onTouch) |
The problem is, I can't find a way to make it wait a little while after touched to perform the action again. The sound plays repeatedly so long as the player is touching the brick. I thought the obvious solution was to add a wait() after when the sound plays, but that didn't work, nor did making it wait until the player is finished touching it to execute again. It may be a simple answer, but how can I fix this?
Hi SuperJumpman12,
01 | local SP = script.Parent |
02 | local deb = false ; |
03 | local S = SP.S; -- The sound I assume. |
04 |
05 | function onTouch(hit) |
06 | if deb then return end |
07 |
08 | deb = true ; |
09 | local H = hit.Parent:FindFirstChild( "Humanoid" ) |
10 | if H ~ = nil then |
11 | S:Play() |
12 | end |
13 | end |
14 |
15 | script.Parent.Touched:connect(onTouch) |
01 | local SP = script.Parent |
02 | local deb = false ; |
03 | local S = SP.S; -- The sound I assume. |
04 |
05 | function onTouch(hit) |
06 | if deb then return end |
07 |
08 | deb = true ; |
09 | local H = hit.Parent:FindFirstChild( "Humanoid" ) |
10 | if H ~ = nil then |
11 | S:Play() |
12 | end |
13 |
14 | wait(S.TimeLength); |
15 | deb = false ; |
16 | end |
17 |
18 |
19 |
20 | script.Parent.Touched:connect(onTouch) |
Thanks,
Best regards,
~~ KingLoneCat
Debounce.
01 | local debounce = false |
02 | local SP = script.Parent |
03 |
04 | function onTouch(hit) |
05 | if debounce then return end |
06 | debounce = true |
07 | local H = hit.Parent:FindFirstChild( "Humanoid" ) |
08 | if H ~ = nil then |
09 | SP.S:Play() |
10 | end |
11 | wait( 1 ) -- Change this to how long you want the cooldown to last for. |
12 | debounce = false |
13 | end |
14 |
15 | script.Parent.Touched:Connect(onTouch) -- connect is deprecated; use Connect. |
01 | local SP = script.Parent |
02 | local Buffer = false |
03 | function onTouch(hit) |
04 | local H = hit.Parent:FindFirstCHild( "Humanoid" ) |
05 | if H and Buffer = = false then |
06 | SP.S:Play() |
07 | Buffer = true |
08 | SP.S.Ended:connect( function () |
09 | Buffer = false |
10 | end ) |
11 | end |
12 | end |
Try this. Didn't test it, let me know if it works.