I have thought of this but what I’m doing is comparing 2 tables to see if they are the same, if they aren’t then make them the same:
Example: Table1 is default and Table2 is not the same as default, finds the values that are missing in Table2 and add them.
for i,v in pairs(rootTable) do
local valueFound
if type(v) == "table" and not v["Stats"] then
local tableExists = v
repeat wait()
for i2,v2 in pairs(tableExists) do
if type(v2) == "table" and not v2["Stats"] then
tableExists = v2
elseif type(v2) == "table" and v2["Stats"] then
valueFound = v2["Stats"]
end
end
until not tableExists or valueFound
print(valueFound)
end
end
Something like this is recursive but it is excessive and unnecessary - you should never be in a position where the amount of sub-tables you have is so needlessly complex that you need to use a solution like this.
Much better to write something along the lines of:
local Tbl = {[1] = {}, [2] = {},[3] = {}}
-- And indexing as:
print(Tbl[1],Tbl[2],Tbl[3])
Than it is to write something like:
local Tbl = {[1] = {[2] = {[3] = {}}}}
-- And indexing as:
print(Tbl[1],Tbl[1][2],Tbl[1][2][3])
I have that in mind, I plan on changing the keys to something like [“LetterProgress”] or something. However I would still not have [“Letter”] or [“Quests”]
There is no method available by default to solve this problem. However, I had this problem before so, I made a function to solve it for me. This may be of help x3
local selectField = function(targetTable, ...)
for layers, key in next, {...} do
local succ, _ = pcall(function() targetTable = targetTable[key] end)
if not succ then
error(("attempt to index nil with %s. %d layers deep."):format(typeof(key), layers), 2)
end
end
return targetTable
end
-- example
local a = { b = { c = { 1 } } }
print(selectField(a, "b", "c")) --> 1
-- example2
local Table = {
Stats = {
Stat = "randomValue?"
}
}
local Stats = selectField(Table, "Stats")
print(Stats.Stat) --> randomValue?
If you don’t want it to throw a petty error because at some point in the nest it indexed nil, replace the error with a return. Most likely, a error message and also a boolean that is false. Then, change whenever successful it returns the value and true.