Music System Error

I want to be able to make a Music System with commands and dev products but I came across an error I’ve been stumbled upon for a while, Sound.Ended:Wait() doesn’t function. It doesnt print “Song Ended” and leads to failure of switching the song

If you have time to answer this; what would be the best way to have a developer product for adding a song?

--//SONG LIST//--
local SongDatabase = {639750143, 279206904, 2164151618}



--//LOCALS//--
local SongName = game.ReplicatedStorage:WaitForChild("SongString")
local SongNumber = 0
local Sound = script.Parent
local MPS = game:GetService("MarketplaceService")

--//SCRIPT//--
while true do
	SongNumber = math.random(1,#SongDatabase)
	print("Song Picked")
	local SongInfo = MPS:GetProductInfo(SongDatabase[SongNumber])
	SongName.Value = SongInfo.Name
	Sound.SoundId = "rbxassetid://"..SongDatabase[SongNumber]
	print("Song ID Changed")
	Sound:Play()
	print("Song Playing")
	Sound.Ended:Wait()
	print("Song Ended")
	
end
1 Like

I would replace Sound.Ended:Wait() with wait(Sound.TimeLength).

Using wait(sound.TimeLength) would not let me add a skip command later on though hence I used .Ended to see when the song stopped

I am pretty sure you can still use a skip command even with wait(sound.TimeLength).

You really can’t. Wait yields the thread for the length of time that the song lasts for, so if the script is relying on the song to end to continue any further functions, it will be hard-anchored to wait for the amount of seconds in wait PLUS extra to find an open slot in the Task Scheduler before resuming.

Wait has little to no place in production code and should be avoided if you can take an event-based approach. Suggesting the use of wait over an event readily provided by the engine is bad advice.

Personally, I am having the same problem with the same thing, but I believe I have thought of a way with using changed property on a boolvalue, then change a local to true then after the wait and it if the local = false it will function a new song.

To be clear idea was completely random.

Example my code;

Yes. I know this topic is old, but maybe this help. :smiley:

Final product;