Gui loading assets

Hi i want to make a loading bar thats shows the progress of loading assets.
The progress bar doesn’t “grow” and there isnt any errors in the output

localscript:

local contentProvider = game:GetService("ContentProvider")
game.Loaded:Wait()
local toLoad = workspace:GetDescendants()
local total = #toLoad
for i,v in pairs(toLoad) do
	script.Parent:WaitForChild("Bar").Size = UDim2.new(i/total, 0, 1, 0)
	contentProvider:PreloadAsync({v})
end

image

image

2 Likes

I believe the problem here has to do with the game.Loaded event rather than the code that follows it. As you probably already know, game.Loaded:Wait() will yield the current thread until the game has loaded. However, let’s say, for instance, the game loads before line 2 of your LocalScript is reached. If that’s the case, your script will be yielding forever waiting for the game to load! To combat this, I suggest placing a check. If the game hasn’t yet finished replicating all of the Instances to the client, yield the code using the Loaded event. If it has, then don’t. The following code demonstrates this:

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

As you can see, the script checks to see if the game has already loaded. If it hasn’t, the script yields until it has. If the game has already loaded, don’t bother yielding the script because it will be yielding forever, waiting for the event to fire.

In context, the code should look like this:

local contentProvider = game:GetService("ContentProvider")
if not game:IsLoaded() then
	game.Loaded:Wait()
end
local toLoad = workspace:GetDescendants()
local total = #toLoad
for i,v in pairs(toLoad) do
	script.Parent:WaitForChild("Bar").Size = UDim2.new(i/total, 0, 1, 0)
	contentProvider:PreloadAsync({v})
end

Hopefully this works. Let me know if you need further clarificaiton.

5 Likes