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

Function waits a bit after brick is touched?

Asked by 7 years ago

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:

01local SP = script.Parent
02 
03function onTouch(hit)
04    local H = hit.Parent:FindFirstChild("Humanoid")
05    if H ~= nil then
06        SP.S:Play()
07    end
08end
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?

0
Thank you to everyone who answered! I had no idea about the debounce function. SuperJumpman12 43 — 7y
0
No problem. KingLoneCat 2642 — 7y

3 answers

Log in to vote
0
Answered by 7 years ago

Hi SuperJumpman12,

This problem can be easily solved by using a debounce. You basically set a boolean value in the function and check for it at the beginning of the .Touched function. So, if that boolean value is a certain value, then the rest of the function runs, otherwise it won't run. Here, it's easier if I just demonstrate it.

01local SP = script.Parent
02local deb = false;
03local S = SP.S; -- The sound I assume.
04 
05function 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
13end
14 
15script.Parent.Touched:connect(onTouch)
View all 22 lines...

If you don't want to use the .Stopped event, you can just add a wait in the function as a cooldown. Here, I will show you below how to do that in the function.

01local SP = script.Parent
02local deb = false;
03local S = SP.S; -- The sound I assume.
04 
05function 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;
16end
17 
18 
19 
20script.Parent.Touched:connect(onTouch)

Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

Ad
Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
7 years ago
Edited 7 years ago

Debounce.

01local debounce = false
02local SP = script.Parent
03 
04function 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
13end
14 
15script.Parent.Touched:Connect(onTouch) -- connect is deprecated; use Connect.
Log in to vote
0
Answered by 7 years ago
01local SP=script.Parent
02local Buffer=false
03function 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
12end

Try this. Didn't test it, let me know if it works.

0
Btw, this will only allow the sound to play again after it is fully finished played (so it will never overlap). optiplex123 21 — 7y

Answer this question