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

local Part = script.Parent
local Sound1 = script.Parent.Sound1


Part.Touched:connect(function(hit)
    local hum = hit.Parent:WaitForChild('Humanoid')
    if hum then
        while true do
            Sound1:Play()
            wait(1)
            --wait(0.82800000000000007)
            break
        end

    end
end)

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.

local Part = script.Parent
local Sound1 = script.Parent.Sound1
local deb = false


Part.Touched:connect(function(hit)
    local hum = hit.Parent:WaitForChild('Humanoid')
    if hum and deb == false then
        deb = true
        Sound1:Play()
        wait(1)
        deb = false
        Sound1:Stop()
    end
end)

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