Random music player breaking

coroutine.wrap(function()
    while playingSong == false do

        playingSong = true
        
        local randomSong = musicFolder[math.random(1, #musicFolder)]
        
        if not randomSong.IsLoaded then 
            randomSong.Loaded:Wait() -- Will keep yielding infinitely if song can't be downloaded
        end
            
        randomSong:Play()
            
        task.wait(randomSong.TimeLength)

        playingSong = false
        
        task.wait(NEXT_SONG_TIME_WAIT)
    end
end)()

I have this random music script but it stops working if a sound failed to load or got moderated. How can I have a check to see if the sound didnt load properly before playing it?

repeat
    local sucess, errormsg = pcall(function()
        -- Load the song here
    end)
until sucess
1 Like

Sorry, I’m a bit unsure of how this would work. Could you please explain how this would check for the sound not loading properly before playing it?

it will keep trying until the song loads

What if the song never loads because it failed to load will it infinitely keep retrying?

Sure.
Basically, a pcall is a function that is incredibly useful for multiple reasons.
In this case, it will contain the error and keep trying until it doesn’t get an error.
I modified the script so it will give up trying to load the sound, in case there is a different problem, and so it won’t crash the script as easily:

local timeOut = 0
repeat
    wait(0.1)
    local sucess, errormsg = pcall(function()
        -- Load the song here
    end)
    timeout += 0.1
until sucess or timeOut >= 3
1 Like

I’m starting to understand now, thank you for your explanation! So would I just replace,

if not randomSong.IsLoaded then
  randomSong.Loaded:Wait()
end

with the code you provided? As for the -- Load the song here would that just be placing the code after randomSong:Play() in that section? Like:

local timeOut = 0
repeat
    wait(0.1)
    local sucess, errormsg = pcall(function()
        randomSong:Play()
            
        task.wait(randomSong.TimeLength)

        playingSong = false
        
        task.wait(NEXT_SONG_TIME_WAIT)
    end)
    timeout += 0.1
until sucess or timeOut >= 3

Yes, I think that should work.

Why not use randomSong.Ended:Wait()

I think i found the script you are looking for
if there is any type of error that is on my line of code then let me know
What the extention does is it uses marketplace to check if its a sound and then have a delay so it loads if it doesnt load in time it skips it

local Market = game:GetService("MarketplaceService")

coroutine.wrap(function()
	while playingSong == false do

		playingSong = true

		local randomSong = musicFolder[math.random(1, #musicFolder)]

	local Asset =	Market:GetProductInfo(tonumber(randomSong.SoundId),Enum.InfoType.Asset)
		if Asset.AssetTypeId == 3 then
	local	TimesRep = 0
		repeat
		wait(0.1)
		until TimesRep == 25 or randomSong.IsLoaded == true
		if randomSong.IsLoaded then
		randomSong:Play()

		task.wait(randomSong.TimeLength)
		
		playingSong = false
		
		task.wait(NEXT_SONG_TIME_WAIT)
		else
				playingSong = false

				task.wait(NEXT_SONG_TIME_WAIT)
		end
		else
			playingSong = false

			task.wait(NEXT_SONG_TIME_WAIT)
end
	end
end)()