Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to make a sound play once when touched?

Asked by 14 days ago
Edited 14 days 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 467 Moderation Voter
14 days 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 14 days ago
Edited 14 days 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