New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
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 3 months ago
Edited 3 months 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 535 Moderation Voter
3 months 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 3 months ago
Edited 3 months 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