How Do I Make Sound Play Once?

So I’ve got this script here, works fine, but the sounds flips out when I run it. It just starts to play it multiple times and it sounds glitchy and does not even finish the sound. Would you debounce just the sound? Or would that not fix it?

game.Workspace.Part.GlassShatter.Enabled = false

local Part = script.Parent
local sound = Instance.new("Sound", game.Workspace.Part)
sound.SoundId = "rbxassetid://5183344180"

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		sound:Play()
		wait()
		sound:stop()
		Part.Transparency = 1
		Part.CanCollide = false
		wait()
		Part.GlassShatter.Enabled = true
		Part.GlassShatter.Rate = 100
		Part.GlassShatter.Life = NumberRange.new(1,1) --stack ends here
		wait()
		print("Ran")
		Part.GlassShatter.Enabled = false
	end
end)

I think there is a property on the sound thing where you can make it loop or make it so it will not loop and only run once.

1 Like

You will need to add a debounce. Copy this:

game.Workspace.Part.GlassShatter.Enabled = false

local Part = script.Parent
local sound = Instance.new("Sound", game.Workspace.Part)
sound.SoundId = "rbxassetid://5183344180"
sound.Looped = false
local debounce = false

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		sound:Play()
		task.wait()
		Part.Transparency = 1
		Part.CanCollide = false
		wait()
		Part.GlassShatter.Enabled = true
		Part.GlassShatter.Rate = 100
		Part.GlassShatter.Life = NumberRange.new(1,1) --stack ends here
		task.wait()
		print("Ran")
		Part.GlassShatter.Enabled = false
		sound:stop()
        debounce = true
        task.wait(3)
        
	end
end)
2 Likes

The loop is off, it’s because it’s a touch function so it keeps running since multiple body parts are touching it.

This and just use the GlassShater.Stop value as well to make sure if you want.
If you feeling picky use the developer.roblox help page to find more values if that doesn’t help.
I’m also not understanding why you are using “Enabled” for a sound value, when clicking on audio the values are stated there.

This works, but it still runs multiple times since I it’s detecting multiple body parts from a character, how do I make it only detect it once?

So if you mean not run the function ever again:

game.Workspace.Part.GlassShatter.Enabled = false

local Part = script.Parent
local sound = Instance.new("Sound", game.Workspace.Part)
sound.SoundId = "rbxassetid://5183344180"
sound.Looped = false
local debounce = false

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		sound:Play()
		task.wait()
		Part.Transparency = 1
		Part.CanCollide = false
		wait()
		Part.GlassShatter.Enabled = true
		Part.GlassShatter.Rate = 100
		Part.GlassShatter.Life = NumberRange.new(1,1) --stack ends here
		task.wait()
		print("Ran")
		Part.GlassShatter.Enabled = false
		sound:stop()
        debounce = true        
	end
end)

Thanks XD but it’s still playing the sound more than once so I need somehow it to only play the sound once when touched.

Copy this:

game.Workspace.Part.GlassShatter.Enabled = false

local Part = script.Parent
local sound = Instance.new("Sound", game.Workspace.Part)
sound.SoundId = "rbxassetid://5183344180"
sound.Looped = false
local debounce = false

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		sound:Play()
		task.wait()
		Part.Transparency = 1
		Part.CanCollide = false
		wait()
		Part.GlassShatter.Enabled = true
		Part.GlassShatter.Rate = 100
		Part.GlassShatter.Life = NumberRange.new(1,1) --stack ends here
		task.wait()
		print("Ran")
		Part.GlassShatter.Enabled = false
		sound:stop()
        sound:Destroy()
        debounce = true        
	end
end)

So it still keeps playing it for some odd reason, let me mess around with a second.

I think it is because of the .Looped property change it to false

So how do I fix that? Add another Loop false in the function?

No it is in the sound property:
image
Should be false

@dutycall11

You need to make the debounce wait more than task.wait()
The reason for a wait in a debounce is exactly for reasons like multiple hits in a touched function.

I have it as Instance.new so the sound would not be present unless the game is running.

So do you mean add more seconds to the wait?

That’s what I said in the post.

Well I asked because I added more seconds to it and it just plays the sound three times, then runs everything else.

Should I just place the sound in it instead of Instance.new?

Yes you should do it that way
character limit