How to Convert Group of Children to Dictionary

  1. What do you want to achieve?
    I want to convert an instance that can be accessed similarly to referencing a certain part. This makes it easier for me to code with multiple gui elements as well as better optimized loading.

  2. What is the issue?
    I do not know how to go about making this kind of system.

  3. What solutions have you tried so far?
    I have looked into DevForum for help, but most of them are either to check if the instance is a class “Folder” or any other grouping parts

-- This is what I want to try to get if I print the dictionary

-- Parameter takes in one instance
local InstanceAsDictionary = {
		[1] = Instance,
		[2] = {
			["children1"] = {
				[1] = children1,
				[2] = {
					["childrenOfChildren1"] = {
						[1] = childrenOfChildren1,
						[2] = nil -- no children found
					}
				}
			},
			["children2"] = {
				[1] = children2,
				[2] = nil -- no children found
			}
		}
	}

Return InstanceAsDictionary 

Quick update, I might actually not want it to be this style since this may not work when using this in practice. What I want to be able to do is have this load in some kind of dictionary, and I can access this by doing Gui1.Gui2.Gui3 without needing to use Gui1:WaitForChild(“Gui2”

I dont really see why you need to do this, it doesnt seem very efficient, nor worth the time making as its as simple as:

Instance.Child
Instance["Child"]

Which is the exact same thing as:

table.index
table[1]
table["index"]

Indexing the Items like this is much faster, and better Optimized than making an entire table, just to do the exact same thing you could’ve done.

The reason I want to make this system is so I could load in my gui without having the worry of using :WaitForChild() for every possible gui element. A solution I could think of is using this type of dictionary style where all gui would be loaded while at the same time, it can act as if I am trying to reference to a parent’s child.

There you go, that’s the issue - you have an XY problem. That’s why it’s important to include your exact use case - here, you’re asking how to fix your attempted solution rather than on how to fix your exact problem. Including a use case might help someone to point out that the way your attempted resolution is wrong and needs to be changed - like me for example.

If you want to reduce the amount of WaitForChild calls, building an instance tree in a table is not the right way forward. There are issues with that solution, chiefly that you can’t guarantee the Gui subtree has loaded when you build the tree in the first place. It’s also just not necessary to do. Furthermore, building an instance tree does not optimise any work here or improve replication speed.

There are two ways you will want to look into resolving this properly: either a module that can snapshot the instance subtree and take advantage of replication running on a reliable ordered channel or build out your Gui with code using a UI framework such as Roact or Fusion. The first method is pretty easy: you can have the server copy Guis to the client and fire a remote after parenting them or set an attribute and have the client wait to receive the attribute on that instance, with a global identity for good measure.