My script doesn't want to delete sound after finishing

Hi
I’ve got a problem, that my script just don’t want to delete the sound after creating it. No errors in output, its silent.

-- gui transitions
function module.blackCover (duration, easing_style, easing_direction, waiting)
	local sg = Instance.new("ScreenGui", player.PlayerGui) -- screen gui
	sg.ScreenInsets = Enum.ScreenInsets.None
	sg.SafeAreaCompatibility = Enum.SafeAreaCompatibility.None
	sg.ClipToDeviceSafeArea = false
	sg.DisplayOrder = 999
	sg.Name = "backCover animation"
	local image_label = Instance.new("ImageLabel", sg) -- image
	image_label.Image = 0
	image_label.AnchorPoint = Vector2.new(0.5, 0.5)
	image_label.Position = UDim2.new(0.5, 0, -0.5, 0)
	image_label.Size = UDim2.new(1,0,1,0)
	image_label.BackgroundColor3 = Color3.fromRGB(0, 0, 0) -- gradient
	local gradient = Instance.new("UIGradient", image_label)
	gradient.Transparency = NumberSequence.new{
		NumberSequenceKeypoint.new(0, 1),
		NumberSequenceKeypoint.new(0.5, 0),
		NumberSequenceKeypoint.new(1, 1)
	}
	gradient.Rotation = 90
	
	-- tweens
	-- fflags
	local lighting_exposure_after_tween = lighting.ExposureCompensation + 5
	
	local imageT = ts:Create(image_label, TweenInfo.new(duration, Enum.EasingStyle[easing_style], Enum.EasingDirection[easing_direction]), {Position = UDim2.new(0.5, 0, 1.5, 0)})
	local lightExposureT = ts:Create(lighting, TweenInfo.new(duration / 2, Enum.EasingStyle[easing_style], Enum.EasingDirection.In), {ExposureCompensation = lighting_exposure_after_tween})
	local lightExposureT_II = ts:Create(lighting, TweenInfo.new(duration / 2, Enum.EasingStyle[easing_style], Enum.EasingDirection.Out), {ExposureCompensation = lighting_exposure_after_tween - 5})
	
	-- sound
	task.spawn(function()
	local sound = Instance.new("Sound", game.SoundService)
	sound.Name = "wind"
	sound.SoundId = "rbxassetid://105756804398751"
	sound.SoundGroup = game.SoundService.Sounds
	sound.Volume = 1
		sound:Play()
		sound.Ended:Wait()
		sound:Destroy()
	end)

	
	-- playing tweens
	imageT:Play(); lightExposureT:Play()
	
	-- deleting if finished. No reason to leave it.
	imageT.Completed:Once(function()
		sg:Destroy()
	end)
	
	-- playing more tweens
	lightExposureT.Completed:Wait()
	lightExposureT_II:Play()
	
	if waiting then
		lightExposureT_II.Completed:Wait()
	end
end -- function end

This is my module script. it should making transitions in the game for gui. The problem at “local sound = Instance.new(“Sound”, game.SoundService)”
it doesn’t delete the sound.
Before you will call me an idiot, I’ve used task.spawn, because its 1 of the solutions I’ve tried to fix it. I also tried using function “sound.Ended”, or “task.delay”. No, it just doesn’t delete. When I tried to use print after deletion, the print doesn’t appear, so my script just doesn’t even try to delete it, and sound stays forever in the sound service. More animations = less ram left. Bad for optimization.

Does somebody knows how to fix it? I’ll be appericate to anyone with help. Maybe is there a problem I don’t see? Please, help

No idea why this should be done through task.spawn; but anyhow I’d probably try doing it through a function like this:

local sound = Instance.new("Sound", game.SoundService)
sound.Name = "wind"
sound.SoundId = "rbxassetid://105756804398751"
sound.SoundGroup = game.SoundService.Sounds
sound.Volume = 1

sound:Play()

sound.Ended:Connect(function()
	sound:Destroy()
end)

In any case, you can additionally set .PlayOnRemove property of any Sound Instance to true to make it play right after the sound was removed, which I’m utilizing in situations like these, and is in my opinion the best method.

That would look something like this:

local sound = Instance.new("Sound", game.SoundService)
sound.Name = "wind"
sound.SoundId = "rbxassetid://105756804398751"
sound.SoundGroup = game.SoundService.Sounds
sound.Volume = 1
sound.PlayOnRemove = true

sound:Remove()

Let me know if ya run into any other issues, otherwise I don’t see why this wouldn’t work.

1 Like

I knew somebody will ask why I tried task.spawn, well, I tried it as the solution. Also omg sound.Ended:Connect(function()) also gets throwed away, because it doesn’t work too.

BUT I tried your way to just make the sound PlayOnRemove, and just destroy it emmidately, and guess what? It works! Thank you a lot, and I think I will use this method now, pretty good I guess. Thank you again!

1 Like

No problem, always happy to help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.