Sound WON'T Play Again

I was trying to play a sound to the local player, I found a way to stop the music from playing by touching another part, but when I try to touch the SoundGiver again, It won’t play the sound again. This was the script:

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
			sound.Ended:Wait()
		end
	end
end)

I tried a method in a separate part of the script, but it still didn’t work:

1 Like

Seems like the sound might be getting deleted after being fired or it’s lagging. (Recommend trying it again with debounce and seeing if it works then)

When I say lagging I mean that it could be getting fired too much

Where do I insert the debounce? (I’m a beginner programmer)

local db = false

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
            db = true
            wait(1)
            db = false
		end
	end
end)

I tried it, and when I touched the part once, it played, but when I touched it again, the sound played a second time and overlapped the other audio.

How do I make it so it detects if the player already has the sound playing, but if the audio won’t overlap the other one, and another player can still touch the part?

I also need to destroy the sound.

Will this work? (Characterssssssss)

There’s a problem: When The player touches the sound giving part, and they stay on the part, every second the sound will restart. And when I replace the “Wait(1)” with “Sound.Ended:Wait()”, when I destroy the sound and I go back to it, it won’t play again for some reason. Here’s my script:

local CanTouch = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
			script.parent.CanTouch = false
			wait(1)
			script.parent.CanTouch = true
			print("Can Touch!")
		elseif player.PlayerGui:FindFirstChild("SoundEffect") then
			local Sound = player.PlayerGui:FindFirstChild("SoundEffect")
			Sound:Destroy()
		end
	end
end)

Btw, your script did the same thing

Why would you clone the sound to the player gui if you can play it from a local script?

Local scripts don’t work in the workspace

Try change this:

if not player.PlayerGui:FindFirstChild("SoundEffect") then

To this:

if not player:WaitForChild("PlayerGui"):WaitForChild("SoundEffect") then

And this:

sound.Parent = player.PlayerGui

To this:

sound.Parent = player:WaitForChild("PlayerGui")

Set Sound.Looped to true.

It stopped the script from working at all

It’s because you never actually delete the sound so in your if not find sound then do this statement wont run because that sound will always be there.

Simply remove the instance once it has finished playing like so and then it would be able to run again just fine.

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
			sound.Ended:Wait()
            ---Add This:
            sound:Remove() -- Simply removes the instance so it doesn't keep 
            --detecting it in the if statement and doing nothing the second time
		end
	end
end)

So once the sound finishes playing it will remove it from the player GUI and therefore will not be able to be found allowing for the if statement to pass.

I tried it, and when I tried to destroy the sound, and I went back to touch the part again, but it wouldn’t play any more. I’m back at the problem I had when I first posted this article!

That’s because you probably did :Destroy(), make sure you do :Remove() that way you dont destroy the initial variable you are cloning. Very different actions. If that doesn’t work just set a clone variable at the top and in your if statement set a new local variable that refers to the clone and remove that.

I tried this, but it still didn’t work. What am I missing?

local CanTouch = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
			script.parent.CanTouch = false
			wait(1)
			script.parent.CanTouch = true
		elseif player.PlayerGui:FindFirstChild("SoundEffect") then
			local Sound = player.PlayerGui:FindFirstChild("SoundEffect")
			if Sound.Playing == true then
				
			elseif Sound.Playing == false then
				local Sound2 = Sound:Clone()
				Sound2.Name = "SoundEffect"
				Sound2.Parent = player.PlayerGui
				Sound2:Play()
			end
		end
	end
end)

You completely forgot to add the remove sound function I told you to. If you arent removing that instance then after the first time it will ALWAYS detect a child called SoundEffect meaning that script will never run after the first attempt. Delete that script in its entirety and just write what I gave you before in my first post:

script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player.PlayerGui:FindFirstChild("SoundEffect") then
			local sound = script.Parent.Sound:Clone()
			sound.Name = "SoundEffect"
			sound.Parent = player.PlayerGui
			sound:Play()
			sound.Ended:Wait()
            ---Add This:
            sound:Remove() -- Simply removes the instance so it doesn't keep 
            --detecting it in the if statement and doing nothing the second time
		end
	end
end)

Your script still didn’t work.

What was the error message in output?