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

How to make a sound play once when touched?

Asked by 4 years ago
Edited 4 years ago

I tried this but Always when touched it play itself more of the same sounds:

script.Parent.Touched:connect(function(hit)

workspace.Sound:play()

end)

2 answers

Log in to vote
0
Answered by
JakyeRU 637 Moderation Voter
4 years ago

Hello. You have to use Debounce. Here's an example:

local CanPlay = true

script.Parent.Touched:Connect(function(hit)
    if CanPlay then
        CanPlay = false
        YourSound:Play()
        wait(5)
        CanPlay = true
    end
end)

Debounce is like a delay. The Script will not continue until the debounce condition is met.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

make a Debounce stored on a variable, something like this:

local played = false
script.Parent.Touched:Connect(function(hit)
    if played == false then
        workspace.Sound:Play()
        played = true
        wait(2) -- change this for the cooldown of it
        played = false
    end
end)

Pretty basic. Hope I helped.

Answer this question