sound:Play() Only Is Not Working

This is probably a simple problem and I’m just not seeing it! The sound is not playing when I run this script, how do I fix that? It runs if I place it outside the function but I want it to run when the player steps on it. Maybe I didn’t define it properly? Thanks! God bless.

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
		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()
		sound:Play()
		Part.GlassShatter.Enabled = false
	end
end)

Make sure the sound has loaded, there should be a function to preload and a event for when it has loaded

The thing is it loads fine when I place it outside my function when the function runs, but I need to be inside the function and it won’t play any sound when I do that.

Perhaps making an external modual script for sound managment might allow it to play, not worked with sounds since the revamp but if its loaded then it should be able to play even within functions.

Also make sure to check if the conditions are met for you if statement, just add a print in the while its in testing

  1. Be sure the script and or sound isn’t deleted before the sound is done playing
  2. sometimes you have to do “if hit.Parent.Parent” It’s best to add both sometimes.

I think something is either:
Deleting the script, Disabling it, Or deleting it’s parent before the script can finish.

Alright, so I placed it at the top inside the function and it runs. It does keep trying to repeat the sound and it sounds glitchy lol. How do I debounce it, or make it only play once?

That’s the part that I would usually have issue with aswell, The touch event is not that good.

Thankfully! We have more Forums:

1 Like