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
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
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.
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.
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
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
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)