Need help creating a data verification system that uses nested tables

Hello!

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.

Here’s what the default data looks like:

   local defaultData = {
	['General'] = {
		['MusicIDs'] = defaultMusic;
		['Coins'] = 0;
		['Experience'] = 0;
		['RequiredExperience'] = 100;
		['Stage'] = 0;
		['Gamepasses'] = {};
		['Inventory'] = {
			['Pets'] = {};
			['Accessories'] = {};
			['Crates'] = {};
		};
		['BanInfo'] = {
			['Banned'] = false;
			['ExpiryDate'] = 'nil';
			['BanReason'] = 'nil';
        };
        ['Settings'] = {
            ['UIPrimaryColour'] = {
                ['R'] = 86;
                ['G'] = 146;
                ['B'] = 127;
            };
            ['UISecondaryColour'] = {
                ['R'] = 255;
                ['G'] = 255;
                ['B'] = 255;
            };
            ['UIFont'] = 'SourceSans';
            ['GlobalShadows'] = true;
            ['PlayerMode'] = 'Visible';
            ['ResetKeybind'] = 'R;'
        };
	};
	['LoginInfo'] = {
		['TimeStamp'] = os.time();
		['Streak'] = 0;
	};
}

Problem is that I’m not sure how I would have it check for each of these values because of the nested tables. Any help would be appreciated!

2 Likes

Dictionaries work like this

local dictionary = {
   ["Words"] = {
      ["And"] = "a word"
   }
}

print(dictionary.Words.And) -- a word

local word = "And"
print(dictionary.Words[word]) -- a word
2 Likes

Yeah, but I’m not sure how I would go about creating a recursive loop that checks each of the nested loops to ensure the value is there.

for i,_ in pairs(defaultData) do
if i == “General” then
print(i[‘Coins’])
end
end

1 Like

You’d to do something like

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
1 Like

Yeah, but if there’s 15 nested tables, I would have to create 15 if statements which I am trying to avoid.

I posted my original script which I’m trying to improve on here:

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

1 Like

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)
5 Likes
repeat 
   if statement thing
until typeof(v) ~= "table" -- again not sure if its table

Like I said assign a serial then do something like

for i=0, maxSerialNumber do
  for k,_ in pairs(table) do
    if string.match(k, i) then
      print(table[i])
      break
    end
  end
end