How to detect when an audio fails to load?

Whenever a sound fails to load it prints

Failed to load sound rbxassetid://<assetid>: Unable to download sound data 

in the console. Is there a way to detect when this happens through scripts?

I tried checking Sound.IsLoaded but that sometimes gets it wrong because sounds take time to load, and if you don’t wait long enough it might produce a false-positive.

1 Like

Use:

[ParentOfTheSound]:WaitForChild("Sound")

and then check if the sound is loaded (Sound.IsLoaded)

1 Like

If the sound hasn’t loaded by the time it’s found by WaitForChild then it will be counted as a fail, even if it loads later

1 Like

How much time your sound loads?

Simply add wait(TheTimeTheSoundTakesToLoad)

1 Like

the problem is I don’t always know how long it will take for the sound to load
the speeds that sounds load can be affected by internet speed for example

2 Likes

Nevermind, check this: Sounds and music

You can use the module Roblox made in this article!

1 Like

That will say “Could not find audio asset” if you didn’t set it up using AudioPlayer.setupAudio, but if you did set it up and the asset fails to load (for example if it was content deleted) it will just wait forever

Basically i need to be able to know when it’s going to wait forever and stop

1 Like

I am unsure as I haven’t tested but I speculate that you can check the song length or volume to see if it fails to load.

1 Like

wouldn’t this have the same problem of not knowing how long to wait before checking?

1 Like

You could try using a loop to check if the sound is loaded or not using a timer that counts up for every second that IsLoaded is false.

local Sound = YourSoundHere --Change this to where your sound is
local LoadTimeout = 120 --This is how long the script will give for the sound to load in seconds until assuming it has failed (2 minutes should be long enough, right?)
local LoadTime --Just declaring this variable since we will use it later...
repeat
LoadTime = LoadTime + 1
wait(1)
until Sound.IsLoaded == true or LoadTime == LoadTimeout
If LoadTime == LoadTimeout then
print("Sound probably has failed to load, timeout reached!")
else
print("Sound loaded successfully!")
end

You can call :Play() with .Volume = 0 and check the value of .IsPlaying. If it’s false then sound failed to play. You can put it inside pcall.

It looks like even if Sound.IsLoaded = false and Sound.Volume = 0 it will still attempt to play the sound and set Sound.IsPlaying to true

Just use ContentProvider:PreloadAsync()
If it returns ID, Enum.AssetFetchStatus.Failure then you can’t load the sound.

local Sounds = {script.Sound1, script.Sound2}

game:GetService("ContentProvider"):PreloadAsync(Sounds, print)

Since it’s asynchronous, I recommend wrapping it with Promises.

7 Likes

Thanks, the docs didn’t say anything about the callback function having parameters

1 Like

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

Use events instead of ‘busy’ waiting.

2 Likes