How do I correctly find subtables?

Hello, I was wondering if anyone can help me to figure out my problem with finding settings in a players’ saved data.

I am currently using this table:

TemporarySaveTable = {["Music"] = false, ["GUI Scale"] = 100}

I am trying to find the value of “Music” and I have tried using table.find but I keep getting nil out of the output.

Can’t you just do TemporarySaveTable["Music"]?

1 Like

… or just TemporarySaveTable.Music

1 Like

I use the bracket operator because it also works with names that contain whitespaces. i.e TemporarySaveTable.GUI Scale doesn’t work. But again, opinions are subjective, so whatever floats your boat.

1 Like

I see that TemporarySaveTable.Music returns as false, thanks!

Do you know how I could go about getting all of the subtables in the table?

for i,v in pairs(table) do
  if type(v) == "table" then
     --do something with table
  end
end
2 Likes

Perfect, that works. Thank you all for helping me.

Also, if your table has multiple levels, you can use a recursive function:

local function RecursiveSearch(aTable)
    for key, value in pairs(aTable) do --unordered search
        if(type(value) == "table") do
            RecursiveSearch(value)
        else
            --Do something with this.
        end
    end
end
1 Like

Ok, I will keep that in mind thanks. I am newer to tables and working with them, I’ve only started coding a year ago and I should have probably started with tables!

1 Like