Troubles fixing Loading assets script

Hello, I have been browsing the forums for an answer and I came across @MrOofBoyPlayz and it pretty much helped me in a way but there are some things I cannot figure out.

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
  1. How do I tween a frame to expand which goes with the items being loaded?
  2. How do I make the GUI disappear after everything is loaded?

PS: Right now, it will load everything I have and just stay there. Like, (Loaded 1/8806 - Loaded 8806/8806)
Thank you for reading this and I hope you can help me!

So, what you want to do firstly is make a frame with the anchor point set to 0, 0. You can put it wherever and put a backdrop if you like.

local frame = --your frame path 

You then want to make your display text percent a variable:

local percentLoaded = math.floor((loaded))/math.floor((totalAssets)) 

And make a function to tween said value, if you don’t know how to tween, watch My Video:

local TS = game:GetService("TweenService") 
local function tweenLoadingFrame(percent)
    local properties = {Size = Udim2.new(percent, 0, 1, 0)
    local info = TweenInfo.new(.5, Enum.EasingStyle.Linear)
    local tween = TS:Create(frame, info, properties) 
    tween:Play()
end

And so, in every for loop you have created, replace

    loaded = loaded + 1
    script.Parent.Text = "Loaded "..math.floor((loaded))/math.floor((totalAssets))

with

loaded = loaded + 1 
local percentLoaded = math.floor((loaded))/math.floor((totalAssets)) 
tweenLoadingFrame(percentLoaded) 

That should work (You could also keep the text label as well).
I have not tested this, so let me know how it goes.
I would also recommend that instead of using a seperate for loop for each asset group, you turn it into a function:

local function loadAssets(assets) 
for _, stuff in pairs(assets) do
    cp:PreloadAsync({stuff})
    local percentLoaded = math.floor((loaded))/math.floor((totalAssets)) 
    tweenLoadingFrame(percentLoaded) 
end
end

loadAssets(assets1) 
loadAssets(assets2) 
--So on and so forth 

Hope I could help, make sure to comment if you have questions or need me to elaborate!

There’s something that I’d like to address by looking at the root of this instead of your specific issue and that’s your use of PreloadAsync right now.

Please, please, please, never use this code in production. It defeats the purpose of PreloadAsync entirely and doesn’t use it in its proper capacity. Here are two gotchas to know:

  • PreloadAsync is meant to prioritise the downloading of assets in your game. Prioritising everything is the same as prioritising nothing. It’s not worth doing this just to try and get a loading bar.

  • zeuxcg previously commented about the practice of preloading items one-by-one instead of an all-at-once operation. In short: try to avoid this.

  • PreloadAsync will already automatically preload both the object you pass as well as its descendants. Preloading is intended for assets that must be downloaded from the CDN and won’t help with anything else (e.g. replication). Please never use GetDescendants with it.

In terms of your bar, addressing the actual question at hand, this is fairly easy to handle. The formula is current/maximum which should give you a decimal between 0-1 assuming you’re staying within the range of 0 < current < maximum. Anything beyond that could produce numbers you don’t want (and you can keep within that range by using math.clamp). This value will serve as your X or Y scale.

Tweening is as simple as using one of the GuiObject tweening methods or TweenService, with recommendation to the latter since that’s intended to encompass and further build upon working with tweens for Guis, instances and more. Same goes for making it disappear: just use the properties window or apply your own fade. Please attempt these yourself before asking for help.

1 Like

Do you have some code I can follow off with? I dont really understand PreloadAsync.

Best way to start is by reading the documentation.

The use of PreloadAsync is correct here in that you’re passing instances in a table to the function, but the application of it in the system is wrong: more specifically, troublesome. You will only ever need to call PreloadAsync on things players need to see immediately: for example, loading screen textures.

In that case, what could I tweak in the code?

You can start by lowering the amount of objects you give to PreloadAsync significantly as well as terminating all instances of GetDescendants. My previous posts should help you work towards reducing that and I’ve also included information about creating a loading bar with the current code.

Please try and make an attempt first at applying the feedback to modify the code if you feel the feedback is worth making changes to your code, then ask regarding your attempted implementation.