Updating Dictionaries when saved to datastore already

I have a dictionary that stores skills in the game. I can add skills and it works with my automated systems.

The issue is if I created a dictionary and save it to the datastore, it works perfectly fine. But what if an old player joins the game with an outdated dictionary that was saved for them? Let’s say we have this dictionary that saves:

The player joins the game and now they have this saved to their datastore as their Skills:

local Skills = { 
	Ice = {Skill1 = "Freeze", Skill1Owned = false}
}

But what if I update the game and I add a new skill. Now the dictonary looks like this:

local Skills = { 
	Ice = {Skill1 = "Freeze", Skill1Owned = false,
    Skill2 = "Ice Breath", Skill2Owned = false}
}

But the original player that joined won’t have the new dictionary of skills, they will have the old one. If I update the dictionary to the new one, it would lose their stats. Like if they own each skill or not (hence the true and false values)

I tried finding solutions on this but I didn’t find any.

Any help is appreciated. Thank you.

You can run through the player’s data and check for inconsistencies based on a template. Here’s an example:

local defaultSkills = {
	Ice = {Skill1 = 'Freeze', Skill1Owned = false,
		Skill2 = 'Ice Breath', Skill2Owned = false}
}

function CheckConsistency(sample, template)
	for a, b in pairs(template) do
		if sample[a] == nil then
			sample[a] = template[a]
		end
		if type(b) == 'table' then -- loop through any nested tables as well. In this case, 'Ice' is a nested table
			CheckConsistency(sample[a], template[a])
		end
	end
	return sample
end

local updatedInfo = CheckConsistency(YourDataHere, defaultSkills)
4 Likes