So I am making a loading screen for my game, because I need to wait until all UI in the game is loaded. The problem is the UI only loads when a player clicks it and puts it into his view. This causes the ui to look bad and leaves a bad user experience. How would I make the UI load and detect when all UI is loaded?
I think you could theoretical do this as simple solution
Put this at the beginning of the script
Repeat
Wait()
Until script.parent:FindFirstChild("name here") and .........(etc.)
(Sorry for typos, mobile)
I have a lot of image buttons, I would have to do it for all of them, but this might be the solution!
If you count how many children are in your UI
You could do
Repeat
Wait(1)
Until #Script.parent:GetChildren() > (number)
Side Note: use
print(#Script.parent:GetChildren())
if you don’t want to count manually
Note that you have to place that script on the main frame, you could modify this however to be in any position, telling the script where the main frame is:
Relative:
Script.parent.parent
Absolute:
game.StarterGui:FindFirstChild(<the frame>)
I’m pretty sure this question goes in Scripting Support.
But how would I do this, use i,v in pairs?
I tried this
local ContentProvider = game:GetService(“ContentProvider”)
local loadtable = {}
for i,v in pairs(script.Parent.Menus:GetDescendants()) do
if v:IsA("ImageLabel") or v:IsA("ImageButton") then
table.insert(loadtable, v)
end
end
function Loaded()
print("Loaded")
end
ContentProvider:PreloadAsync(loadtable, Loaded())
print(#script.Parent.Menus.Shop.Crates:GetChildren())
but its printing only 1 for the bottom print statement. It is being loaded.
There is no way to detect when UI loads, only when they replicate. There is however a way to detect when an asset loads provided you preload it via ContentProvider. As far as it’s use in a loading screen goes, you should only be preloading assets you need seen immediately - anything else can wait and be streamed in.
Typically for a loading screen I will make it relatively blank except for some aesthetic indicators (the same as how almost every game, Roblox or not, sets up a loading screen). From there, I will pass anything seen immediately into PreloadAsync and the rest I just let stream in on use.
A note about PreloadAsync (cc @TheCarbyneUniverse): PreloadAsync accepts a table of instances, but know that the descendants of those instances are also passed. There is virtually no reason to ever use GetChildren/Descendants: pass the top-level object in the table and the rest will follow. I never recommend passing an entire container like PlayerGui either, as that’s not proper use of PreloadAsync.
So there is no way to do this? The only problem I am having that I want to fix is that I have a bunch of buttons inside a few image frames, and for that, I use for i,v in pairs, but the script ends up going through 0 items because they aren’t loaded. I printed a get children statement, but when it should have printed 5, it printed 0.
If you want to wait until the Instance’s are actually in the DataModel, simply use :WaitForChild("ButtonName")
before you move onto the code that uses these buttons. Even if all the buttons are the same name, this should still work fine.
I got the other thing to work, as I was trying to access something else with the same name. But I still want to know how to load the UI?
What do you mean how to load the UI? You need to be specific. Are you trying to load roblox-assets such as images? Are you trying to replicate some ScreenGui as fast as possible on load?
If you are trying to create a loading screen that will load as possible, use ReplicatedFirst.
Call game.ReplicatedFirst:RemoveDefaultLoadingScreen()
to remove the loading screen. Then, assuming you have some ScreenGui inside of ReplicatedFirst (and a LocalScript in ReplicatedFirst), just parent the ScreenGui into PlayerGui. This will Replicate as soon as possible and start running.
I just want to load the images, as they are not being loaded until the player clicks on the button and it comes into view, which looks bad because half the ui is still loading.
I know I’m super late to the party, but I have a theory.
Since you can’t load any UI without the StarterGui loading first, wouldn’t this quick snip of code do the trick?
local pg = player:WaitForChild("PlayerGui")
repeat wait() until #pg:GetDescendants()>=#game.StarterGui:GetDescendants()
--do something here
I’ve tested it a bunch of times and it never seems to error.
By all means, correct me if i’m wrong.
You can count the number of children. for example if you have 100 gui objects.
when you do :GetDescendants and it only has 46 of them you can tell that 56 of them have not loaded.
from this you can work out the % it has loaded as well. 46/100 * 100 = 46%
so you can give loading bars / percentages.
PreloadAsync is an asynchronous operation that is used to load assets from instances like your UI image buttons/labels. It will yield until all the assets related have been pre-loaded.
Ultimately, there is not much of a way to know whether normal UI elements like TextButtons and TextLabels have loaded but rather assets which can include image assets.
Yah that’s what we do for Sk sword fighting, thought it was a unqiue method but guess a lot of people use it lol.
One way I would check is in studio, I would count the amount of ui’s then wait until the player has loaded that same amount. Seems neat for this post
ik that we are now in 2021 and that u posted this topic in 2019 but u can use YOUR_INSTANCES_HERE.IsLoaded()