Hello. I’m trying to fill in missing template names if I ever were to update my data template to prevent existing data from not loading. But now I’m stuck and can’t find it out.
local playerData: any
local getSuccess, errorMessage = pcall(function()
playerData = dataStore:GetAsync("Player_"..player.UserId)
end)
if not playerData then
playerData = dataTemplate
end
for dataType, _ in dataTemplate do
if not playerData[dataType] then
playerData[dataType] = dataTemplate[dataType]
warn("Added type: "..dataType)
end
for dataName, _ in dataTemplate[dataType] do
-- I want to add every missing data name to every data type aswell, but I don't know how to do this.
end
end
Are you going to use the standard DataStore? Why not use Profile Service? It’s easier for you to load or save data. It has template/table that you can store the way you want.
I’m trying to do it my own way, not having to require the ProfileService module a lot of times. I only need to load player’s data once and set their attributes.
I’ve tried this and it results in all data that have set booleans (instead of numbers) in the template to get added. I have only removed my ‘‘Time’’ from my data for testing, so it should only add time.
If player’s data have been saved prior, and I decide to add, let’s say a new currency called gems. I want the script to set the player’s data ‘‘gems’’ to 0 by copying the template.
local playerData: any
local getSuccess, errorMessage = pcall(function()
playerData = dataStore:GetAsync("Player_"..player.UserId)
end)
if not playerData then
playerData = {}
end
for dataType, data in pairs(dataTemplate) do
if not playerData[dataType] then
playerData[dataType] = data
warn("Added type: "..dataType)
end
for key, value in pairs(data) do
if not playerData[dataType][key] then
playerData[dataType][key] = value
end
end
end
Wouldn’t this only add the types like ‘‘Boosts’’ and ‘‘Currency’’? I don’t think the script will notice if I were to update the game and add gems inside of the template’s ‘‘Currency’’ table, because currency already exists for the player.