Hey,
I want to get all descendants of a table, but it’s not possible to get them by using ‘:GetDescendants()’.
The purpose is to use a for loop and check every “descendants” name (the current index) and do smth with those who have a specific names.
Exampe:
local x = {
a = 1,
b = 2,
c = 3,
d = {a = 5, b = 10}
}
So in this particular example I’m trying to get every descendant with the name “a”. But their located in different tables. I don’t know how many there are and don’t want to count them out for very huge tables.
You will need a recursive function. It will work with a lot of layers too,. Here is a pseudo-code of that
-- Here I have the output where all the values in the table will be
local descendants = {}
local x = {
1,
2,
{3, 4}
{5, 6, {7, 8}}
}
function recursiveGetDescendants(table)
for i, v in pairs(table) do
if type(v) == "table" then
--Since it is a table, loop then through THAT table too.
recursiveGetDescendants(v)
else
--Comes here if the value is NOT table, so we just add it to output
table.insert(descendants, v)
end
end
end
-- Call the function to add all of X's values into the table.
recursiveGetDescendants(x)