Basically, I am trying to create a system that checks if each part of the user’s data is in accord with the default data that is given to users when they join, thus if I create a new value to save later, it will be saved. The problem is that I am not sure how I would go about doing this due to the issue of nested tables.
local dictionary = {
["Words"] = {
["And"] = "a word"
}
}
print(dictionary.Words.And) -- a word
local word = "And"
print(dictionary.Words[word]) -- a word
for i,v in pairs(defaultData) do
if typeof(v) == "table" then -- not sure if it's table, don't have time right now
for k,o in pairs(v) do
-- compare values and repeat everything from the if typeof
end
end
i is the memory address, since we assigning the memory address a string for its name instead of 0xedafjhdjkaf we simply just treat i as a string and compare it to the keys u got. We can assign what I like to call “string tags” which is basically where we add for ex “Coins-1” then use string.match(string, “Coins%-1”). For a actual example with full code check out how I implemented it here https://github.com/EppersonJoshua/rasterizer-roblox
I know how tables/dictionaries work, I am just trying to figure out a way to iterate through each table that is within a table to check that all of the values are there.
So say if I’m missing defaultData.General.Settings.UIPrimaryColour, it would default. I don’t want to create a large strand of if statements because that looks messy and is difficult to read.
Recursion is complex at face value but it isn’t terribly difficult in the thick of things.
Here’s a simple function that will run through each and every table inside of the “ancestor” table.
local tbl = {
["A"] = 1;
["B"] = {
["C"] = 2;
["D"] = {
["E"] = 3;
}
}
}
function Sort(tbl)
for i,v in pairs(tbl) do
if typeof(v) == "table" then
print(tostring(i)..": table")
Sort(v)
else
print(tostring(i)..": "..tostring(v))
end
end
end
Sort(tbl)