How to make sure everything is fully loaded

Hello

I’m creating a loading screen and I want to make sure that everything like the children of every model is loaded so I can avoid infinite yield. Is there any workaround?

2 Likes

For workspace, you can do

local Assets = workspace:GetDescendants()

for i = 1, #Assets do
	local Asset = Assets[i]
	game:GetService("ContentProvider"):PreloadAsync({Asset})

	if i % 5 == 0 then
		wait()
	end

	if i == #Assets then
		wait(1)
	end
end

If you want to include the whole game, simply change Assets to be game:GetDescendants()

2 Likes

i mean id say so but also you your loop make sure you use ipairs because for loops can iterate over tables in any order so it could crash sometimes

corrected code:

local Assets = workspace:GetDescendants()

for i = 1, ipairs(#Assets) do
	local Asset = Assets[i] -- because here your getting a key value and without the ipairs the loop runs in any order and it could give weird results
	game:GetService("ContentProvider"):PreloadAsync({Asset})

	if i % 5 == 0 then
		wait()
	end

	if i == #Assets then
		wait(1)
	end
end
2 Likes

Not sure how this is corrected or what that changes, there is no need for it to be ordered.

1 Like

actually i guess ur right nvm sorry its just a habit :joy:

2 Likes

You can use RequestQueueSize, which is a part of ContentProvider. This property stores how many instances are missing in the game. You can make a loop, which waits for RequestQueueSize to be equal to 0. Here is what I mean:

print("Loading has started!")
local contentProvider = game:GetService("ContentProvider")
while contentProvider.RequestQueueSize ~= 0 do
	task.wait()
end
print("Loading has ended!")

RequestQueueSize shouldn’t be used like this, it is also stated in the creator’s hub.
“Developers are advised not to use RequestQueueSize to create loading bars. This is because the queue size can both increase and decrease over time as new assets are added and downloaded.”

For a loading screen, getting all the assets you want to load and using PreloadAsync() on them is one of the best ways.

2 Likes

instead of forcing everything to load, why not just do a game.Loaded event?

1 Like