Why Isn't My Script Detecting When a Sound Ends?

I am making a music script and I can’t figure out how to detect when the currently playing sound ends. Here is a snippet from my script:

chosen:Play() -- A defined Sound
print("Music has started.") -- Prints
repeat wait(3) until chosen.Playing == false
print("Music has ended.") -- Doesn't print

As you can see in the comments from the code snippet above, the second print statement does not execute; however, the first print statement does execute. I tried using this as well but the same problem occured:

chosen:Play() -- A defined Sound
print("Music has started.") -- Prints
chosen.Ended:Wait()
print("Music has ended.") -- Doesn't print

All help is greatly appreciated!

There are 2 things.

  1. Sound.Ended does not run if the sound is looped
  2. if the sound is stopped, you will need to use sound.Stopped to detect it.

check here for more.

sound.Ended:Wait() yields the function until the sound ends. So replace your loop with that.

Thanks for replying!

The sound is not looped.

I am not stopping the sound, the sound ends automatically after completing.

That’s what it’s supposed to do. However, when the sound ends, Sound.Ended:Wait() keeps infinitely yielding. And no, the sound is not being deleted at any point during this process.

Strange, I experienced no issues with it. Maybe try sound.DidLoop or connecting sound.Ended to a function.

sound.Ended:Connect(function()
	print("Sound Ended")
end)

Also you might want to add sound.Loaded:Wait()

Sadly, using a function would not work as this is occuring in a while true do loop.

  1. Functions do work in loops
  2. Since it is a loop, it would continuesly play the music, so it might not even end as it is being played over again and again.

Also send me the entire script or revelant parts of the script, so I can anaylize the issue.

Nevermind, I found out that it somehow wasn’t triggering the Ended event because I was skipping the song (changing the timeposition of the sound). Thanks anyway @doytnr

Glad you found out! Next time please send the entire script so that people trying to help you have some context on the situation.