Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 .

1 comment

0

@deerdharok Thanks so much, this really helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.