Help with Sound on Part Touch

I have this code im using for making sound jumpscares that play when you touch the part. It is a localscript in startercharacter because i want only the player who touches the part at the moment to be able to hear it.

My problem is that i want the sound to activate only once (only reset the trigger to work again if the player has died) but as of right now idk how. When i remove the wait the other parts that trigger sounds on touch stop working. Help would be greatly appreciated!

local played = false

workspace.JumpscaresChapter1:FindFirstChild("JumpscareChurch").Touched:Connect(function(hit) --Which jumpscare we coding
	if played == false and hit.Parent:FindFirstChild("Humanoid") then
		workspace.JumpscaresChapter1:FindFirstChild("GlassBreak"):Play() --these are Sounds and The Folder in the workspace
		played = true
		wait (5) --This Is delay after first play, you can change it if you want to
		played = false --Ready to play again
	end
end)

workspace.JumpscaresChapter1:FindFirstChild("JumpscareCrematoriumStairs").Touched:Connect(function(hit) --Which jumpscare we coding
	if played == false and hit.Parent:FindFirstChild("Humanoid") then
		workspace.JumpscaresChapter1:FindFirstChild("ScreamMonster"):Play() --these are Sounds and The Folder in the workspace
		played = true
		wait (5) --This Is delay after first play, you can change it if you want to
		played = false --Ready to play again
	end
end)

workspace.JumpscaresChapter1:FindFirstChild("JumpscareCrematorium1").Touched:Connect(function(hit) --Which jumpscare we coding
	if played == false and hit.Parent:FindFirstChild("Humanoid") then
		workspace.JumpscaresChapter1:FindFirstChild("Chirrido"):Play() --these are Sounds and The Folder in the workspace
		played = true
		wait (5) --This Is delay after first play, you can change it if you want to
		played = false --Ready to play again
	end
end)

workspace.JumpscaresChapter1:FindFirstChild("JumpscareCrematorium2").Touched:Connect(function(hit) --Which jumpscare we coding
	if played == false and hit.Parent:FindFirstChild("Humanoid") then
		workspace.JumpscaresChapter1:FindFirstChild("ScreamMonster2"):Play() --these are Sounds and The Folder in the workspace
		played = true
		wait (5) --This Is delay after first play, you can change it if you want to
		played = false --Ready to play again
	end
end)

workspace.JumpscaresChapter1:FindFirstChild("JumpscareCrematorium3").Touched:Connect(function(hit) --Which jumpscare we coding
	if played == false and hit.Parent:FindFirstChild("Humanoid") then
		workspace.JumpscaresChapter1:FindFirstChild("SteamPipe"):Play() --these are Sounds and The Folder in the workspace
		played = true
		wait (5) --This Is delay after first play, you can change it if you want to
		played = false --Ready to play again
	end
end)

If you want each of these to only be played once, you’ll need a ‘played’ variable for each of them ,separately. Also, you won’t want to reset the played = false (to make it ‘ready to play again’ as your script comment states).

If you won’t ever use the connection again, it would probably be best just to disconnect it after its triggered.

I think that by putting this in startercharacter, as you say you did, will make the script start over when they reset, so you wouldn’t have to worry about coding the reset into the script.

1 Like

Thanks for the help, it worked just as i wanted :slight_smile: