Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[SOLVED] Storing instance descendants in a table?

This may sound stupid and unnecessary, however what I'm trying to do is send the descendants of an instance (like workspace) from the client to the server. Using :GetDescendants() I get all the descendants, and that's good, but if I send that to the server, like this:

-- remote function, client side
return workspace:GetDescendants()

And then print the values on the server, like this:

-- remote function, server side
for i, v in pairs(returnedValue) do
	print(v.Name)
end

The names printed are only those replicated server-side. I have no way to see the instances that are ONLY on the client-side.

So I had the idea of creating a table with all the instances and their children, and then send that to the server. So I made this:

function EncodeDescendants(ins) --Base instance as arg#1, returns table
	local descTable = {
		["Name"] = ins.Name,
		["ClassName"] = ins.ClassName,
		["children"] = {}
	}
	local childrenToAdd = {}
	--
	for _, v in pairs(ins:GetChildren()) do
		table.insert(childrenToAdd,#childrenToAdd+1,{
			["instance"] = v,
			["parent"] = descTable
		})
	end
	--
	repeat
		local newChildrenToAdd = {}
		--
		for index, v in pairs(childrenToAdd) do
			local child = v.instance
			local childParent = v.parent
			--
			local newChildInfo = {
				["Name"] = child.Name,
				["ClassName"] = child.ClassName,
				["children"] = {}
			}
			--
			table.insert(childParent.children,1,newChildInfo)
			--
			for _, v in pairs(child:GetChildren()) do
				table.insert(newChildrenToAdd,1,{
					["instance"] = v,
					["parent"] = child
				})
			end
		end
		--
		childrenToAdd = newChildrenToAdd
	until #childrenToAdd == 0
	--
	return descTable
end

But it gives an error: Line 29: invalid argument #1 (table expected, got function) childParent.children should be a table, not a function. Any help is appreciated.

1 answer

Found a solution, if anyone is interested this is the fixed script:

function EncodeDescendants(ins)
	local descTable = {
		["Name"] = ins.Name,
		["ClassName"] = ins.ClassName,
		["childrenTable"] = {}
	}
	local childrenToAdd = {}
	--
	for _, v in pairs(ins:GetChildren()) do
		table.insert(childrenToAdd,1,{
			["instance"] = v,
			["instanceParent"] = descTable
		})
	end
	--
	repeat
		local newChildrenToAdd = {}
		--
		for _, v in pairs(childrenToAdd) do
			local child = v.instance
			local childParent = v.instanceParent
			--
			local newChildInfo = {
				["Name"] = child.Name,
				["ClassName"] = child.ClassName,
				["childrenTable"] = {}
			}
			--
			local childChildrenTable = childParent["childrenTable"]
			table.insert(childChildrenTable,1,newChildInfo)
			--
			for _, val in pairs(child:GetChildren()) do
				table.insert(newChildrenToAdd,1,{
					["instance"] = val,
					["instanceParent"] = childChildrenTable[1]
				})
			end
		end
		--
		childrenToAdd = newChildrenToAdd
	until #childrenToAdd == 0
	--
	return descTable
end

Now I will have to find a way to get all the instance properties in the table. If I find a way to do that (and I don't forget) I will put it here.

This discussion is preserved as it was. New answers, comments, and votes are closed.