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)
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
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
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 .
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
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.
My game (Noobs vs Zombies Realish Reborn - Roblox) has recently been having this issue! Many mechanics rely on the .Ended event to fire for music queues, chargeup attacks, etc so this is a big problem that has been causing frustrations for my players!
Most instances of me coding this are as simple as
Sound:Play()
Sound.Ended:Wait()
So I don’t know why it isn’t really working so good as of recent. Players reported that this began happening a few days ago, so I suspect it has something to do with the recent audio changes that have been happening.