I’m not 100% sure if this is what you’re looking for but you’re trying to ensure a table has all values, and if not you want to insert it?
I wrote a function that does this and relies on recursion so you can do it to any depth of a table, it takes 2 arguments: first argument is the table to be verified, or the table you want to insert values for, and the second table should be the table that is to be used to verify that the table to be verified, is verified if that makes sense.
So for example, let’s say I have this as my default table:
local defaultTable = {
['Coins'] = 0;
['OwnsCoolItem'] = false;
['Level'] = 0;
['SomeValue'] = 1234;
['SomeTableWhereValuesAreInserted'] = {
['number1'] = 1;
};
}
Then the player joins, their data looks like this:
local playersData = {
['Coins'] = 12345;
['OwnsCoolItem'] = false;
-- oh no the player's missing the Level value!
['SomeValue'] = 123456;
['SomeTableWhereValuesAreInserted'] = {
['number6'] = 123456;
['number2'] = 12345678;
-- oh no they're missing an index number1!
}
}
you can run it through this function and the output should be:
local playersData = {
['Coins'] = 12345;
['OwnsCoolItem'] = false;
['Level'] = 0;
['SomeValue'] = 123456;
['SomeTableWhereValuesAreInserted'] = {
['number6'] = 123456;
['number2'] = 12345678;
['number1'] = 1;
}
}
Here’s the function:
local function verifyCurrentTable(tableToVerify, currentIteration) -- tableToVerify is where we would pass playersData, currentIteration is where we would pass defaultTable
for i,v in pairs(currentIteration) do -- we have to use pairs because we're often working with dictionaries
if tableToVerify[i] == nil then -- if inside the tableToVerify, it's missing a value then
tableToVerify[i] = v -- insert the default value
elseif type(v) == 'table' then -- if the value is a table, call this function but with the current table
tableToVerify[i] = verifyCurrentTable(tableToVerify[i], v)
end
end
return tableToVerify
end
It’s also important to note that if you’re ever to remove a value manually, it will keep reappearing due to it detecting a missing value.
Let me know if you need help with getting it set up, or if I completely overlooked what you’re looking for.