How would I go about comparing two tables and filling in values that one table doesn't have that another table has?

Pretty confusing title, let’s say I have a template for my Datastore and I wanted to add a new stat to the template. How would I go about adding the new stat to the player’s data? (which is a table)

You have 3 ways of adding data in a table:

First is by an Index (Index is considered to be a value like string or number in the squared brackets)

[1] = value

Second is to add it as a property. Use it when you have just 1 value and not another table in the current table as it is done for readability. Also don’t use it when iterating threw different items at once.

.Apple =

Third is basically the first however you add it at the end of the table. Use it when you want to add a lot of items and you would be deleting some from before by index.
table.insert(, “Apple”)

In this case you would add to the word string “Apple”.

Usually I have a DEFAULT_DATA module. You can add whatever new keys and values you want. Then when a player logs in you apply to them the default data and pull out their data from the DS and for each key that’s in their DS data update their default data.

If I’m understanding this correctly, you can use table.clone and clone the template with the new index into the new table.

Yep you could do
local newtable = oldtable.clone()
newtable would have the same values as oldtable and would have completely different references.

Well, here is something that I used for my game’s data updating. May look messy, but it does the job.

local function gothrough(path, tbl)
	for i,v in pairs(tbl) do
		path[i] = path[i] or v

		if type(v) == "table" then
			gothrough(path[i], v)
		end
	end
end

gothrough(data, defdata)

for reference, data is the player’s data, and defdata is the default data a player would start with.

I think I get what you mean (by reading the title).

function idkName(tab1,tab2)
	local a,b = table.clone(tab1),table.clone(tab2)
	
	for i, v in a do
		if b[i] == nil or b[i] ~= a[i] then
			table.insert(b,i,v)
		end
	end
	
	for i, v in b do
		if a[i] == nil or a[i] ~= b[i] then
			table.insert(a,i,v)
		end
	end
	
	return a,b
end

local a = { 1,2,5 }
local b = { 1,3,4 }

print(table.concat(a,','),table.concat(b,',')) -- 1,2,5 1,3,4

a,b = idkName(a,b)

print(table.concat(a,','),table.concat(b,',')) -- 1,2,5,3,4 1,2,5,3,4
-- Declare the template table
local template = {
  stat1 = 0,
  stat2 = 0,
  stat3 = 0
}

-- Declare the player's data table
local playerData = {
  stat1 = 5,
  stat2 = 10
}

-- Iterate through the template table and add any missing keys to the player's data table
for key, value in pairs(template) do
  if playerData[key] == nil then
    playerData[key] = value
  end
end

-- Now playerData should contain all keys from the template table, with their corresponding values from the template or the player's data, whichever is available
print(playerData.stat1) -- Output: 5
print(playerData.stat2) -- Output: 10
print(playerData.stat3) -- Output: 0

This approach will add all missing keys from the template table to the player’s data table, with the corresponding values from the template. If the key already exists in the player’s data table, it will not be modified.