Need help making a loading screen that shows the amount of objects that need to be loaded

I want to make a gui say how much objects it’s loaded. Like the gui will get how many objects there is in the assets table (for example, it will get the amount of stuff in the workspace) and say “Loaded 0/[Amount Of Objects]”

But… when I tried it, it’s max is 7 which is the amount of lists in the assets table, I don’t want that. I want it to show how much objects it’s loaded and how much objects there is.

Here is the code:

local assets = {
    game.Workspace:GetDescendants(),
    game.Lighting:GetDescendants(),
	game.ReplicatedStorage:GetDescendants(),
	game.StarterGui:GetDescendants(),
	game.Players:GetDescendants(),
	game.StarterPack:GetDescendants(),
	game.Players.LocalPlayer.Character:GetDescendants(),
}

local cp = game:GetService("ContentProvider")
local totalAssets = #assets
local loaded = 0

for _, stuff in pairs(assets) do
    cp:PreloadAsync(stuff)
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end
2 Likes

ContentProvider:PreloadAsync takes in an array of instances and waits (yields) until they are loaded. So you can just have a table of parts and iterate over it using a for loop, while using PreloadAsync to wait until the part is loaded, then after, each PreloadAsync you can add to the number of assets loaded. I would recommend checking out these links (they might give you a better idea of what I’m talking about): Creating a Loading Bar based off of number of assets loaded - #3 by Fm_Trick : ContentProvider | Documentation - Roblox Creator Hub

You’re on the right track tho.

If you need further help i might be able to assist

1 Like

Wait I tried the solution on that link/post but it stills returns a max of 7…

So yeah, I might need some extra help lol

Yeah of course it would return 7 since you have 7 objects in the assets table. If you want more then you would just need to reference other objects or just do

local assets = game.Workspace:GetChildren()

or you can just make the for pairs a few times in your script until you get all the services objects loaded.

But why can’t I use :GetDescendents()

You can use that, it’s just that I prefer using :GetChildren().

1 Like

But did that! It still returned 7…

Because you have 7 tables in the assets table.

2 Likes

Ahhhh ok :smiley: :slight_smile:

@uhi_o Nope still 7 as max. Why does it do this?

Try this

local assets1 = game.Workspace:GetChildren()
local assets2 = game.ReplicatedStorage:GetChildren()
local assets3 = game.StarterGui:GetChildren()
local assets4 = game.Lighting:GetChildren()

local cp = game:GetService("ContentProvider")
local totalAssets = #assets1+#assets2+#assets3+#assets4
local loaded = 0

for _, stuff in pairs(assets1) do
    cp:PreloadAsync(stuff)
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets2) do
    cp:PreloadAsync(stuff)
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets3) do
    cp:PreloadAsync(stuff)
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets4) do
    cp:PreloadAsync(stuff)
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

I know this code is real inefficient and not neat but I couldn’t think of the better way right off my head since it’s late where I am.

Edit: Also maybe add a coroutine? In case you want it to execute at same time.

2 Likes

What is a coroutine? Is it a quarantine for scripts? :joy:

(JK but still…)

How would I make this a coroutine?

Also, thanks for writing this for me :slight_smile:

It doesn’t work, it is stuck at 0

Edit: Unable to cast to Array, I can fix this.

Edit again: Ok fixed.

Ok now the percentage goes up (and removes the gui) before the Loaded 0/76 thing goes up. I need some help here, I think I needa do that coroutine. Can someone help me do it?

Here is the current script:

local assets1 = game.Workspace:GetDescendants()
local assets2 = game.Lighting:GetDescendants()
local assets3 = game.ReplicatedStorage:GetDescendants()
local assets4 = game.StarterGui:GetDescendants()
local assets5 = game.Players:GetDescendants()
local assets6 = game.StarterPack:GetDescendants()
local assets7 = game.Players.LocalPlayer.Character:GetDescendants()

local cp = game:GetService("ContentProvider")
local totalAssets = #assets1+#assets2+#assets3+#assets4+#assets5+#assets6+#assets7
local loaded = 0

for _, stuff in pairs(assets1) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets2) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets3) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets4) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets5) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

for _, stuff in pairs(assets6) do
    cp:PreloadAsync({stuff})
    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
end

(This is in a different script.)

Why make it co routine there is no point of stopping the main thread (the script) and starting a new thread for the co routine and then waiting until it stops. First the co routine is suspended then started after it runs its course it becomes dead (Unless you yield it i just think you shouldn’t load any instances except textures if you have any because on mobile platforms it will take much much longer so just use normal time a variety it i don’t want to be stuck for like 4 minutes if i am on phone.

Roblox already loads stuff inside the game but stuff like textures u need to load with content provider

How do I make a coroutine?

coroutine(function()
--Code here?
end)

Like that?

So i think you should just make a variable that has the max time u want and the min time and it finds what time to use

 local timetaken = math.random(1,100) -- 100 is the max time you want 1 is the min number but it will find a number between 1 and 100.

No:

coroutine.create(function()
	for _, stuff in pairs(assets1) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
	end
	
	for _, stuff in pairs(assets2) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
	end
	
	for _, stuff in pairs(assets3) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
	end
	
	for _, stuff in pairs(assets4) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
	end
	
	for _, stuff in pairs(assets5) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
	end
	
	for _, stuff in pairs(assets6) do
	    cp:PreloadAsync({stuff})
	    loaded = loaded + 1
	    script.Parent.Text = "Loaded "..math.floor((loaded)).."/"..math.floor((totalAssets))
		end
end)

Is this right?

1 Like

No coroutines are like this

local newThread = coroutine.create(function()
    print("Hi!!!")
end)

Bear in mind this will not run i just created it.