GetDescendants behavior for Tables?

i’ve started working with Tables not that long ago, so it’s entirely possible the issue could be dumb or complex : i’m trying to fully put the content of a table into one TextLabel like the following :
image
(NOTE : This TextLabel’s output are for a single Table (Inventory Only), my goal is to make “Analyse Full Data” Working.)

To be more direct, i’m looking for a way to get the same behavior as:GetDescendant(), except it would be used for Tables instead of Instance

in short, i have tables in a table, and would like to be able to print whatever’s inside the source, just like brick-counting Workspace.
image

i know it doesn’t look hot, but it’s pretty much what i’m trying to print in one go

What i have tried so far are unpack(table), which couldn’t actually be set as a Text, so i’ve used table.concrat to do the Text spacing for each item. however (obviously), didn’t worked to open a table with concrat.

2 Likes

You can loop through the table, and check if type(v) == "table" we loop through it as well to show its content.

for i, v in pairs (inventory)
if type(v) == "table" then
   for i, sv in pairs(v) do
      --do the stuff
    end
else
   --do stuff
end
end

This is kind of inefficient, but did you try tostring(unpack(table))? Maybe that would work, I’m not sure what type of value unpack returns but I think it’s just mixed up because there are different values in the table, so it would return a bunch of values put together like this 7, "example", true. I’m not sure if all values are converted into one string, or each one is on its own. Or perhaps it’s a tuple, which is another datatype. (a tuple is basiclly a bunch of values put together, just like what unpack returns)

1 Like

A recursive approach will make sure that it it collects all descendants, and not just it’s children.

function doStuff(index, value)
	print(index, value)
end

function recurseTable(tbl, func)
	for index, value in pairs(tbl) do
		if type(value) == 'table' then
			recurseTable(value, func)
		else
			func(index, value)
		end
	end
end

local targetTable = {
	['a'] = 'Pizza',
	['b'] = 'Please',
	['Extra Requests'] = {
		'No meat',
		'Extra veggies',
	}
}
recurseTable(targetTable, doStuff)
14 Likes

Sorry for not replying earlier, i was trying to figure out how to print it alongside the source’s name (table’s name & others) and @VitalWinter sent a solution including it, Thanks alot ! :confounded:
image

1 Like

I have a script that pretty-prints a table recursively. Example of output:
2019-09-11_10-37-39
The function is a bit long to put here (around 80 lines) but I can send it to you in a private message if you want.

1 Like

Thanks for the offer, unfortunately, it won’t be of use since i mentionned as introduction that the final product appears as a TextLabel and not as a Print
image

means that getting all childrens of a table was my final objective.

No problem! Pleasure to help with this

The code serializes the table as a string so you can easily assign it to the Text of your TextLabel. I suggested using my function because it preserves the hierarchy of the table.

Can I have the function? It would be of use for me.