Game crashing when using PreloadAsync

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Fix this weird crash issue

  2. What is the issue? Include screenshots / videos if possible!

--

local ContentProvider = game:GetService("ContentProvider")

--

ContentProvider:PreloadAsync({script.Parent.Background.Pattern})

--

local Remotes = game.ReplicatedStorage:WaitForChild("Remotes")

--

local Event = script.StartLoading

--

local LoadingStarted = false

local StatsLoaded = false

--

Remotes.LoadingStatsFinished.OnClientInvoke = function()
	StatsLoaded = true
	
	return true
end

--

local loop = false

local showProgress = false

--

local folder = game.ReplicatedStorage:WaitForChild("Assets")

local toLoad = #folder:GetDescendants()

local loaded = 0

task.spawn(function()
	for i, thing in folder:GetDescendants() do
		ContentProvider:PreloadAsync({thing}, function()
			loaded += 1
			if showProgress then
				script.Parent.progress.Text = tostring(loaded)
			end
		end)
	end
end)

--

Event.Event:Wait()

showProgress = true

--

LoadingStarted = true

loop = not StatsLoaded

while loop do
	task.wait(3)
	
	loop = not StatsLoaded
end

repeat task.wait(1) until loaded == toLoad

print("loaded!")

script.LoadingFinished:Fire()
  1. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Adding waits

The game crashes every single time this loop stops

while loop do
	task.wait(3)
	
	loop = not StatsLoaded
end

I’m on vinegar (linux mint) so that could be part of it but I haven’t add any other crashing issues
don’t judge my code I’m planning on rewriting it later just working on the base for a game

After looking at your code, the reason for crashing is very likely not coming from that while loop itself. I believe the issue is this part:

for i, thing in folder:GetDescendants() do
	ContentProvider:PreloadAsync({thing}, function()
		loaded += 1
	end)
end

You’re calling PreloadAsync once per asset inside a loop. That means if you have hundreds or thousands of assets, you’re yielding repeatedly and stacking a ton of preload calls.

PreloadAsync is already a blocking yield. It waits until the content is loaded. So you’re just effectively doing yield on yield hundreds of times.

The proper way to do this is to preload everything at once.

Also another thing I noticed is this line:
repeat task.wait(1) until loaded == toLoad

If loaded never exactly equals toLoad, you’ll soft-lock forever.

Try safer version like this:
repeat task.wait() until loaded >= toLoad

1 Like

alright thanks however it’s been crashing even before I did

for i, thing in folder:GetDescendants() do
	ContentProvider:PreloadAsync({thing}, function()
		loaded += 1
	end)
end

I just did this before

ContentProvider:PreloadAsync(game.ReplicatedStorage:WaitForChild("Assets"):GetDescendants())

it crashes the second that loop stops and I can’t figure out why (it works completely fine if I comment the preload out)

1 Like

PreloadAsync should only be given to instances that actually contain content (images, sounds, meshes, animations.) If your Assets folder contains things like:

  • Folders
  • Scripts
  • Values

They will still be returned by GetDescendants() but aren’t valid preload targets, which can cause weird stuff to happen. So I recommend filtering them by doing something like:

for _, obj in ipairs(folder:GetDescendants()) do
	if obj:IsA("Decal") or obj:IsA("Texture") or obj:IsA("Sound") or obj:IsA("MeshPart") then
		table.insert(assets, obj)
	end
end

Or simply just remove anything else in your Assets folder.

Try these debugs:

  1. Preload a small subset of assets
  2. Filter valid asset types
  3. Make sure you’re not relying on loaded == toLoad exactly.
1 Like

alright thanks I’ll try it

it worked thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.