How can I get all of the objects in workspace and their children and put them into a table?

Hi, as the title says, I am trying to figure out how to get all of the objects and their children into a table.

By this I mean:

local table = {
     object1 = {

I have tried to use for loops, but there were some complications of objects getting put in three or four times

Does anyone know?

local getTable
getTable = function(entry)
    if not #entry:GetChildren() > 0 then return entry end
    local r = {}
    for i, v in pairs(entry:GetChildren())
        --table.insert(r, getTable(v))
        --you could do an array assignment like above if you prefer.
        r[v.Name] = getTable(v)
    return r

If you’d normally want to get all children, you would normally use :GetChildren(), however since you want to get the children inside of the objects you have to use :GetDescendants.

So in your case this would look like:

local table = {workspace:GetDescendants()}

1 Like

GetDescendants is best if it can be used, but it returns a single list of items rather than a nested list.

1 Like

So this might sound a little dumb, but I was wondering what the func(v) is here for, is it short for function or something? you changed it lol

I messed up. It’s recursive so it’s calling itself. I just renamed the function and forgot to fix it. It’s been updated.

local function GetTreeTable(Start)
	local tbl = {}
	for Index, Obj in pairs(Start:GetChildren()) do
		local Descendants = GetTreeTable(Obj)
		if Descendants then
			tbl[Obj.Name] = Descendants
	return tbl

This one did seem to work the best, thanks