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?
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
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)()