Should I create multiple Datastores?

Scenario:

I make a game with a single datastore that stores a table. The table:

local Data = {["Coins"] = 0, ["Wins"] = 0,}

But after launch, I want to make an update that implements gems to the game. Now the new template is:

local Data = {["Coins"] = 0, ["Wins"] = 0,["Gems"] = 0}

However, there is a problem, “Gems” is not stored in the old players data

This looks like a rather simple problem, one solution I’d do would be making a new datastore for gems, but, what happens when in the next update you want to add 2 things, then the next one 1 etc…
You would end up with a lot of datastores, wouldn’t you?

My question is: Is there a way to implement the new data into the Data table so the older players also get the update?

So what you wanna do is basically insert new values to the Player’s Data table because it is what ur update is going to feature. So you just get the table while player added
and then set a new key in it, like the following and save it to the datastore / the variable that its stored in while player is in game.

if not Data["Gems"] then
   Data["Gems"]  = 0
end

Basically what the above does is check if the key Gems doesn’t already exists, and adds that key to the Data Table with a value of 0.

Then you just update and whatever, then you save it as normal.

1 Like

I have thought of this but that will soon become around 30 lines just from adding 10 things to the datastore (In my case it’s gonna be a lot since I’ll be storing settings).

My idea is to have a function that checks if their data has all the keys that are in the default data, I coded this so you know what I mean:

function GetDefaultData()
  return {["Coins"] = 0, ["Wins"] = 0,["Gems"] = 0}
end

function Check(SavedData)
  local DefaultData = GetDefaultData()

  if #SavedData == #DefaultData then
    return SavedData
  else
    for Name, Value in next, DefaultData do
      if not SavedData[Name] then
        SavedData[Name] = Value
      end
    end

    return SavedData
  end
end

and when you’re getting their data from the data store you can use something like this:

local SavedData

local Success, ErrorMessage = pcall(function()
  SavedData = SavingDataStore:GetAsync(PlayerKey)
end)

if Success then
  local CheckedData = Check(SavedData)
  return CheckedData
else
  warn(ErrorMessage)
  local NewData = GetDefaultData()
  return NewData
end
2 Likes

You could add the new Keys you wanna add to a table, and then loop over them and check them one by one too.

1 Like

Ah thank your for coding this, very clear explanation and easy to understand thanks!

1 Like