How to check if PreloadAsync Failed?

Should i do this:

for index, value in pairs(game:GetDescendants()) do

repeat

local success = pcall(function()
	contentProvider:PreloadAsync({value})
end)

until success

end

or something like this i saw somewhere:

local function preload(asset, status)
    -- if loading was a success then status will be Enum.AssetFetchStatus.Success
end

local success, err = pcall(function()
	contentProvider:PreloadAsync({value})
end)

if err then 
	--// Preloading failed do something 
end

image

1 Like

But when i dont use repeat and it errors 2+ times it will not loop again

But i saw this somewhere:

So would this work?

for index, value in pairs(game:GetDescendants()) do

repeat

	contentProvider:PreloadAsync({value})

until value.Enum.AssetFetchStatus == "Success"
-- Or
until value.IsLoaded == true

end

Tally up your assets before preload, then just check by making sure your tally matches the number of assets that loaded, or add to a counter during preload and compare with that value!

PreloadAsync has a second argument for a Callback function.

local function Callback(Id: string, Status: Enum.AssetFetchStatus)
	if Status == Enum.AssetFetchStatus.Failure then
		
	end
end

ContentProvider:PreloadAsync(IdList, Callback)

Do i need Id: string? Can i just make string or nothing there, just Status?

PreloadAsync doesn’t error so pcall is useless.
And you don’t need to create a new function inside of a pcall just to call another function.

pcall(CProvider.PreloadAsync, CProvider.PreloadAsync, {value})

Thanks but why is in the script u gave me a pcall then?

I did this:

for index, value in pairs(script.Parent.Parent:GetDescendants()) do
	print(1)
	contentProvider:PreloadAsync({value}, function()
		
		task.wait(100)
		
	end)

end

But the Loop is not pausing when the function is waiting, how can i pause the Loop?

It’s not pausing because the callback function runs on different thread.
It’s like you did task.spawn().

Pausing the loop is not a good idea, especially because most of the time when PreloadAsync fails, it means Roblox deleted the resource so you won’t preload it anyway.

I would rather recommend waiting a few seconds and repeating the request once after the first one fails.

But it also fails when the resource is not deleted, like when somebody has a bad Internet