Sound.Ended is broken

Reproduction Steps

This has been posted before, but got locked, claiming that I just need to read the responses for the answer. But none of the responses actually provide any answer.

The marked solution literlaly says “:Ended is called when the sound actually ends” which my sound does end and it still doesn’t fire.

CurrentSong.Ended:Connect(function()
    print("Done") -- Never prints
end)

Expected Behavior
.Ended SHOULD fire when the sound ends, but it doesn’t

Actual Behavior
Nothing happens, that’s the problem

Workaround
No

Issue Area: Engine
Issue Type: Other
Impact: High
Frequency: Constantly
Date First Experienced: 2017-12-17 00:12:00 (+08:00)

Can’t seem to recreate?

script.Sound:Play()
script.Sound.Ended:Connect(function()
	print("Done") -- Never prints
end)

Can you provide a full script? Or studio file that has the issue?

Song:Play()

CurrentSong = Song

if not CurrentSong.Looped then -- Not a looped song (plot music)
	print(CurrentSong) -- prints
	
	CurrentSongLoopConnection = CurrentSong.Ended:Connect(function()
		print("done", CurrentSong.Name) -- never prints
		local SongIndex = string.gsub(CurrentSong.Name, "%D", "")
		--print(SongIndex)
		if not SongIndex then return end
		--print(tonumber(SongIndex))
		if not tonumber(SongIndex) then return end -- Not a number
		
		local NextIndex = tonumber(SongIndex) < 3 and tonumber(SongIndex) + 1 or 1
		--print("Next song is", NextIndex)
		
		local NextSong = Music:FindFirstChild("Plot-" .. NextIndex)
		SoundPlayer.PlayMusic(NextSong)
	end)
end

Are you letting it end, or are you stopping it?

Letting it end. I don’t call :Stop() anywhere

Still can’t recreate:

You are aware that you’ll have to reconnect Ended when you play the next song unless you use one sound instance and change its SoundId, right?

Can’t really assist unless you give an actual full script or recreation file.

I only play 1 sound.

function SoundPlayer.PlayMusic(music)
	local Song = Music:FindFirstChild(music)
	if not Song then return end
	
	if CurrentSong then -- Currently a song playing, fade out
		local OriginalVolume = CurrentSong.Volume
		
		local TweenOut = TweenService:Create(
			CurrentSong,
			TweenInfo.new(1),
			{
				Volume = 0
			}
		)
		TweenOut:Play()
		TweenOut.Completed:Wait()
		
		print("STOP")
		CurrentSong:Stop()
		CurrentSong.Volume = OriginalVolume
	end
	
	if CurrentSongLoopConnection then
		CurrentSongLoopConnection:Disconnect()
	end
	
	Song:Play()
	
	CurrentSong = Song
	
	if not CurrentSong.Looped then -- Not a looped song (plot music)
		print(CurrentSong)
		--// Continue playing plot music
		CurrentSongLoopConnection = CurrentSong.Ended:Connect(function()
			print("Stopped", CurrentSong.Name)
			local SongIndex = string.gsub(CurrentSong.Name, "%D", "")
			--print(SongIndex)
			if not SongIndex then return end
			--print(tonumber(SongIndex))
			if not tonumber(SongIndex) then return end -- Not a number
			
			local NextIndex = tonumber(SongIndex) < 3 and tonumber(SongIndex) + 1 or 1
			--print("Next song is", NextIndex)
			
			local NextSong = Music:FindFirstChild("Plot-" .. NextIndex)
			SoundPlayer.PlayMusic(NextSong)
		end)
	end
end

Even with all of that I still can’t recreate this:

SoundPlayer.rbxl (36.4 KB)

I have no clue what’s going on then :confused:

When I do CurrentSong.Ended:Wait() it works perfectly fine

Is it possible that you set CurrentSongLoopConnection and then immediately (or at least before the song ends) run the method again which disconnects CurrentSongLoopConnection?

So I recommend instead of being smart and skipping over what I told you in DM, let’s try get a clear repro first, then post it back to the bug report category. Clearly from the responses above something else is going on. Moved to #help-and-feedback:scripting-support .

1 Like

This isn’t a bug .Ended is supposed to fire when a song actually ends and .Looped is set to false, I have replied on your old post showing you all the sound events related to a song stopping:

And the event you’re looking for is the .Stopped event which fires when a song is stopped using Sound:Stop().

Obviously you didn’t read my post. When do I ever call :Stop() in my code?? I am clearly looking to use .Ended. No where in my code do I use :Stop(), nor do I even mention :Stop(). I am not using :Stop() anywhere. The song ENDS and .Ended does not fire

1 Like

Hi NinjoOnline,
We’re unable to reproduce this issue and believe that this is expected behavior.
In the original code there is a potential race condition where the sound, if it is short enough, may start and finish playing before sound.Ended:Connect() is called.
Alternatively, if the sound does not end before sound:Stop() is called, the Stopped event will be called instead of the Ended event.
If you are still experiencing this problem it will help us if you can provide the simplest possible reproducible case.