How would I rewrite a player's DataStore if I change the default format

So basically, assume that when a new person arrives, their data by default is this:

local function NewData()
	local data = {
		Level = 1,
	}
	return data
end

But, when I make a change to the format in a new update, how would I update their data to the new format to avoid errors. Example of the new format:

local function NewData()
	local data = {
		Level = {
			NormalLevel = 1,
			OtherLevel = 1,
		}
	}
	return data
end

Any help is appreciated!

1 Like

Create a version handler. Have a VERSION variable saved in the data store, and if the version doesn’t match the current version, convert it to the new version. In your example, you can create a new table entirely but substitute one of the level variables in the level table with the previous level. Then, once you’re done converting the version, update the version variable.

Check if the data had the old format, if it does convert it to the new one.

if type(<data>.Level) == 'number' then
    -- its old schema
else
    -- New schema
end

i think its fine to use in this case you gave