So, I have this script inside a part called SoundPlayerOne:
local part = script.Parent part.Touched:connect(function(hit) local h = hit.Parent:FindFirstChild("Humanoid") if h then wait(1) local sound = Instance.new("Sound") sound.Parent = part sound.Looped = true sound.Playing = true sound.Volume = 1 sound.SoundId = "rbxassetid://178276804" wait(1) end end)
Basically, it makes a Sound and puts it into SoundPlayerOne. There is a problem though, when it gets touched, it inserts 5 sounds into the part because of Roblox physics. I've tried making a version of this script with debounce but it just doesn't work.
How do I make a script where, when a certain amount of children is reached (2 or more in my case); it will delete them until there is one.
If you have an alternative way (i.e. debounce, which I can't seem to get to work) then feel free to answer with that method. :-)
The Touched will fire for every part that touches it e.g. if you walk on a part it will fire for each leg since they are different parts.
You should be using the sound IsPlaying property if you only want to play this when the part is touched. I also do not know why you are making a new sound instance each time.
I cannot tell how you want this code to work since you have not included that information so this will be more of a guess.
local part = script.Parent -- create and parent the sound to our part there is no need to create a new sound instance local sound = Instance.new("Sound") sound.Volume = 1 sound.SoundId = "rbxassetid://178276804" sound.Parent = part part.Touched:connect(function(hit) if not hit.Parent:FindFirstChild("Humanoid") or sound.IsPlaying then return end -- return out of function if humanoid is not found or if the sound is playing -- play the sound sound:Play() end)