Load sound if failed

Can i do this to load the Sound if its failed to load or is it a bad Code?:

repeat

script.Parent.SoundId = “rbxassetid://soundId”

task.wait(0.1)

until script.Parent.IsLoaded == true

Would it work?

1 Like

I wouldn’t do that, I’m not sure why the sound wouldn’t load. Could you possibly show the full script?

1 Like

I’m a bad scripter but try this

if script.Parent.IsLoaded == false then script.Parent.Loaded:Wait()

Yours should be good too tho

1 Like

This happens sometimes in all games its working but in a 1/20 case its failed loading this happend exactly now again it failed loading and it put the Number again inside and the sound is playing now that means the script is working or?

1 Like

That would not work if the sound failed to load

Try this:

local Sound = script.Parent

local function loadSound(sound)
	-- Has the sound already loaded?
	if not sound.IsLoaded then
		-- if not, wait until it has been
		sound.Loaded:Wait()
	elseif sound.IsLoaded then
		-- if sound is loaded, run the code here
	end
end

loadSound(Sound)

Read over an article (Sound | Roblox Creator Documentation), somewhat helps and may help your situation. Update me on how it goes.

1 Like

But this will also not work because if the sound failed to load it will not try again to load

You’re right but the sound will wait to load which is technically the same thing. If you want it to try to load again then it you’ll just have to run it in a loop like so but the difference is practically the same, I’d rather use wait because it makes more sense but try this:

local Sound = script.Parent

local function loadSound(sound)
	-- Has the sound already loaded?
	if not sound.IsLoaded then
		-- if not, wait until it has been
		loadSound(sound)
	elseif sound.IsLoaded then
		-- if sound is loaded, run the code here
	end
end

loadSound(Sound)
1 Like

Okay and could i when i use loops also use runservice.Heartbeat or would it be to much and is task.wait(0.1) better?

I’d use task.wait(0.1) for now. Only because I lack experience with the RunService option but depends what you use it for, don’t overcomplicate things.

1 Like