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

How do I stop a sound from playing multiple times when stepping on a brick?

Asked by 8 years ago
Edited by M39a9am3R 8 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I'm having trouble with a certain script. I don't know how to stop a sound from playing multiple times when you touch a brick. It keeps repeating and I have no answer for this.

This is the whole script v

01local Part = script.Parent
02local Sound1 = script.Parent.Sound1
03 
04 
05Part.Touched:connect(function(hit)
06    local hum = hit.Parent:WaitForChild('Humanoid')
07    if hum then
08        while true do
09            Sound1:Play()
10            wait(1)
11            --wait(0.82800000000000007)
12            break
13        end
14 
15    end
16end)

p.s I'm new to scripting, so go easy on me.

0
It appears you formatted this a bit wrong. Make sure you put your code in between the "~~~" lines. antonio6643 426 — 8y
0
Thank me by accepting my answer Shawnyg 4330 — 8y
0
Make sure you also aren't looping it! To stop this, disable it in Properties. wolfbarrier123 -25 — 8y

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

Once upon a time, there was debounce. Debounce is used to help in these types of situations, by stopping an event from firing multiple times.

01local Part = script.Parent
02local Sound1 = script.Parent.Sound1
03local deb = false
04 
05 
06Part.Touched:connect(function(hit)
07    local hum = hit.Parent:WaitForChild('Humanoid')
08    if hum and deb == false then
09        deb = true
10        Sound1:Play()
11        wait(1)
12        deb = false
13        Sound1:Stop()
14    end
15end)

You also had an unnecessary while loop.

0
It worked out perfect, thanks. Rackinmills 0 — 8y
0
Glad I could help Shawnyg 4330 — 8y
0
Accept his answer. awfulszn 394 — 8y
Ad

Answer this question