I’ve got this table right, but it has tables in that table, some of them ALSO have other tables inside them. But I seem to have forgotten how you find a value in a table, regardless of how many tables it’s in
(i.e., like FindFirstChild([Instance], true) it will find what your looking for no matter how many times its grouped or foldered. That’s what I’m trying to achieve.)
Like I said, I’m looking for a value you in a table that contains tables, and those tables might also contain tables (i.e., like parts in groups of groups, etc), in a loop. It’s a function that acts like FindFirstChild([Instance], true) but for tables. I used to know how to get a value deep within tables of tables, but I forgot.
I don’t understand how there isn’t a :GetDescendants() for tables. Using a for loop on a table that has tables, only gets those tables but if those tables have also got tables inside them, you can reach them without making another for loop, it’s just messy and ridiculous.
Use recursion. For each table, the function calls itself. This function has a for loop where it checks each index of the table, if found the desired value, it returns the value, closing the recursion.
use a recursive function to search the tables @TheRealANDRO beat me to it while i was writing a code example :c
anyway here’s a recursive linear search for a table, tested! It returns the table the value was in, as well as it’s key within that table (if it was found).
local function recursiveLinearSearch<T, V>(searchTarget: T, value: V): ({[any]: any}?, any?)
--search the initial table first
for k, v in next, searchTarget, nil do
--check the value
if (v == value) then
return searchTarget, k --return the table and the index
elseif (type(v) == "table") then --wasn't the value, but it was a table
local tbl, index = recursiveLinearSearch(v, value) --search that table too
if (index) then --if it was found in that table, return it
return tbl, index
end
end
end
--not found
return nil, nil
end
Believe it or not I found a solution which is actually quite similar to yours, although not as fancy and complicated. It’s just a test, so I will obviously improve it, but in case anyone wants to know how to do this here’s a picture of the code:
Basically just loops through a table to find what you’re looking for, if it can’t find it, it will keep trying. The issue with this is it will keep trying and never stop until it finds what your looking for or break the script. May get out of hand. But as I said, it’s just a basic test.
Oh also thank you for your help as well, I can’t believe we both had a similar idea.
Well… As it turns out, me solution didn’t work in my script, but worked in the command bar. So I tried your code, and I got this instead, not sure what’s going on.
It’s a table of many tables, i just put the a, b, and c ones at the bottom and commented the the rest out to see if it where the tables causing the issue, but apparently not.
oh, it’s because you’re looking for the key, not the value. Typically, search algorithms will return the key of the element, and the element is being looked for. So, if you put in true, it would return table c and also findThisOneHere.
So, hence to stick with typical design, my code snippet looks for the value and returns the key.