How do I save new data made in a template to the player?

My issue is that when adding something new to my data template, it wont be saved to an old player’s data.

Heres an example of what i’m talking about.

local PlayerData = { -- example player data
	TableA = {
		"yes",
		"probably",
		"maybe",
	},
	TableB = {
		Cat = 4,
		Dog = 8,
		Other = {}
	},
	Apples = 89
}

and heres the example template.

local DataTemplate = {
	TableA = {},
	TableB = {
		Turtles = 0,
		Other = {},
	},
	Apples = 0,
	Bananas = 2,
}

I want turtles and bananas to be added to the old player’s data in each respective place just like when im configuring my data template I want it to update old player’s data too, how would I do this?

1 Like

You’d have to check for changes to the template data every time a player joins. This would involve recursively checking every key in the template and making sure that the player’s data also has the same key:

 -- Check for new data added to the template. 
local function checkForNewData(playerData, template)
	for key, value in pairs(template) do
		local element = playerData[key] -- index template key in player data
		if not element then -- check if key from template exists in data
			playerData[key] = value -- if data does not exist, initialize template key in player data
		elseif typeof(element) == "table" then -- if the element is a table, then run this algorithm on that table
			checkForNewData(element, value)
		end
	end
end
2 Likes

You should use ProfileStore (formely profile service )its an open source data store module that is really simple to use and has many function including “:Reconcile()” which add’s new tables added to the template to all old datas

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.