Is there an easy way I can determine when a UI has loaded all of it's elements?

Hey, so rather than writing out all of my UI variables like this for example:

local frame1 = gui:WaitForChild("F1")
local button1 = frame1:WaitForChild("B1")

I came up with a wonderful workaround to this. What I do, is I create a recursive function that iterates over the UI, and for each element it finds it’ll recurse that element, and add everything to a table. So when ever I want to access a UI element specifically, I can simply just do the following:

ui = {}
ui.some_frame.Visible = false

Here’s what I’m a bit concerned about though, if when I run this recursion, what if some elements in the UI for that player did not load in? So when I try to access it, it might return nil. And it doesn’t seem super efficient for me to put like a 3 second yield at the top of my script, so I’m trying to find the best possible way I can make sure all the elements in the user interface has loaded in before I begin this recursion. Thanks!

If you use WaitForChild in your recursive function you don’t have to worry about nil values, because the function will continue yielding until the UI element replicates. If you’re worried that some instances will never replicate, then you can use the timeout parameter of WaitForChild(name, timeOutInSeconds), and then record which items timeout in your recursive function.

There’s also this post (How do I detect if all UI is loaded? - #11 by colbert2677) that may help you, but it seems like that post never really got anywhere.

2 Likes