Music Breaking after a while

Hello everyone, I’m currently having an issue with a Music playlist Script.
It is breaking randomly after 5min - 10h when a Song ends. I’m unsure if it breaks when it’s unable to load a Song, or what the issue actually is.

Screens:


Herunterladen (2)

Code:

while true do
	for _,song in pairs(Playlist) do
		wait()
		print("ID: ".. song)
		script.Parent:WaitForChild("Sound").SoundId = "rbxassetid://".. song
		script.Parent:WaitForChild("Sound")
		script.Parent:WaitForChild("Sound").TimePosition = 0
		repeat wait() until script.Parent:WaitForChild("Sound").IsLoaded == true
		script.Parent:WaitForChild("Sound"):Play()
		repeat wait() until script.Parent:WaitForChild("Sound").IsPlaying == true
		local Asset = game:GetService("MarketplaceService"):GetProductInfo(song)
		warn("Playing: "..Asset.Name)
		script.Parent.songname.Value = Asset.Name
		repeat wait() until script.Parent:WaitForChild("Sound").IsPlaying == false or script.Parent:WaitForChild("Sound").TimePosition == script.Parent:WaitForChild("Sound").TimeLength
		wait(0.5)
		workspace.MusicSystem.Sound:Stop()
		wait()
	end
	wait()
end

If you have any other way to make the Playlist more efficiant, feel free to tell me :slight_smile:

Yes, it is unable to load a song as the error states in the console.

There are a few things I would recommend you change.

  1. You can do ipairs instead of pairs if it is just an array.
  2. There’s no need for all the :WaitForChild()s if the script is a child of the Sound.
  3. You can just define the sound instance as a variable at the top of the script instead of writing script.Parent.Sound a bunch of times.
  4. Instead of doing repeat wait() until true where true is the condition, use the :Wait() function on the .Loaded/.Played/.Ended events respectively.
  5. No need to run :Stop() if it does not loop.

There are two reasons:

  1. The song ID is invalid
  2. The song ID is valid but the song is taken down.

Inspect all of your IDs first. You can also check if it’s loaded or not.

You could try using the pcall() function or preloading the soun. Or you could keep the sounds in a folder and use :Play() on them so the script itself never deals with loading the sound IDs.

What would you recommend to fix this problem the easyest?

I think having the sounds in the folder and using the Play() function on them would be easiest. I also just tested the sound ID which was in the error message in the screenshot. It wouldn’t load for me either so you probably need to remove it

If he does do that, I would recommend pairing that with ContentProvider:PreloadAsync().

while true do

local ChosenSong = random(1,NumberOfSongs)

for i, v in pairs(SoundFolder:GetChildren()) do
if i == ChosenSong then
v:Play()
wait(v.TimeLength + 5)
end
end

I would recommend putting that in a codeblock by embedding it in 3 backticks. Also, no need for the for loop. Just store SoundFolder:GetChildren() as a variable then do SoundFolder[ChosenSong]. As well, no need to put a wait(n) in there. The Sound instance has a built-in .Ended event so you can do Sound.Ended:Wait().

Is ther a way to check if a Song is working or not, and otherwhise remove it from the table?

Kinda like

for ipars(songs) do
if failed then
local thasong = find tabe.find(songs, ID)
table.romove(songs, thasong)
else
print(“Fine”)
end
end

You can do that with the callbackFunction parameter of ContentProvider:PreloadAsync().

Worked now.

Thanks a lot for your help.
Songs that don’t work get luckily removed now.