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

How do you keep a sound from repeating?

Asked by
Spooce 78
10 years ago

How do you keep a sound from repeating with an onTouch function? I want it to play once then stop and go again when stepped on again. But instead the sound repeats a bunch of times while I stand on it. Help?

1 answer

Log in to vote
0
Answered by 10 years ago

You can use a debounce to stop it repeating. For making it start/stop when you touch it, use a variable to make the variable true when the sound is playing.

db = false --Debounce variable
playing = false --Checks if sound is playing (there's a property in Sound, but it's buggy)

script.Parent.Touched:connect(function(hit) --Touched event's function.
    if db then return end --Stops the code if db is true.
    db = true --Sets db to true so the code doesn't run multiple times.
    --Optional check to see if hit is a player's character here.
    if playing == false then
        --Play your sound here.
        playing = true
    else
        --Stop your sound here.
        playing = false
    end
    wait(1)
    db = false --Waits 1 second before allowing someone to touch the brick again. Important, otherwise nobody will be able to touch your brick afterwards.
end
Ad

Answer this question