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

Sound only plays once when touching a part, but doesn't play again?

Asked by 6 years ago

I am trying to make it so when a player touches a part, it will make a sound. But when touched again, the sound doesn't play. How do I fix this?

Here's the Script I used:

local debounce = false
function onTouched(hit)
    if debounce == true then return end
        debounce = true
    script.Parent.Music:play()
end
debounce = false
wait (2)
script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
1
Answered by 6 years ago

i was a bit confused by your script, so i changed some values to make it more understandable

local playable = true


function onTouched(hit)

    if hit.Parent:FindFirstChild("Humanoid") then ---checks to see if a player touched the brick
        if playable == true then --- checks if it is able to play
            --- if its able to playe then  run this code
            playable = false

            script.Parent.Music:play()

            wait(2)  ---waits for 2 seconds before being able to play again
            playable = true 
        end
    end


end


script.Parent.Touched:connect(onTouched)

see, when something touches the brick it runs the function "onTouched", so everything inside that function will run, so thats why your debounce didnt reset .

0
@deerdharok Thanks so much, this really helped. HimoutoUmaruDomaChan 20 — 6y
Ad

Answer this question