Datastore Concerns

Datastores have been a concern for me for a long time, and no, it’s not just data loss.
This is a really simple question, will data be lost/mixed up if I were to add new data?


This is my ultimate concern:

Let’s say that this was the data that was going to be saved when the game just got released:

Coins -- Value is 1
Wins -- Value is 2
Losses -- Value is 3

and then save it with SetAsync or UpdateAsync from getting the data stored in a table:

local Statistics = Stats:GetChildren()
local TOV = {}
for i, Stat in pairs(Statistics) do
	table.insert(TOV, Stat.Value)
end
DSS:SetAsync(UniqueKey, Statistics)

the table would look something like this

local TOV = {1, 2, 3}

And then in the future, I add an update that adds a new value:

Coins -- Value is 1
Wins -- Value is 2
DamageDealt -- Value is 0
Losses -- Value is 3

As you can see, table TOV stores the data for Coins, Wins, and Losses, but not for DamageDealt. This is my concern. When the data loads in again, the values would be changed:

Coins -- Value is 1
Wins -- Value is 2
DamageDealt -- Value is 3 [Should be 0]
Losses -- Value is 0 [Should be 3]

Is this a problem? If it is can you overcome it?

If you don’t get my question, I can elaborate it, you just need to ask.


Thank you so much.

1 Like

Instead of using an array, use a dictionary, simple

{coins = 0, wins = 0, losses = 0}

If you add extra data, you could do

if data and not data.DamageDealt then
	data.DamageDealt = 0
end
1 Like

I don’t think that you can save dictionaries in DataStores.

Tes, you can save dictionaries in Datastores.

1 Like

You can store dictionaries, I have done it before

1 Like

you can save a dictionary the same way you can save a table.

a dictionary is good because you’ll always have to specify a ‘key’ for each of its values.

someTable["Coins"] = 100

this (as an example) will always change the ‘Coins’ value, and is impossible for it to become mixed up.

1 Like

In my experience a DataStore issue are caused by the programmer. There can be a lot of factors why things go wrong and some misconceptions.

One of the biggest issue is design the save system as late as possible. So many problems can simply be solved by this. When you have a more complete game you know when, where and how you should load in data and you will have a better idea of what needs to be saved and how oftern data needs to be saved.

For development I would create a module scripts with my test data in which can simply return a table. As long as the table is JSON compatible you will be ok (primative types, number, string, bool, non mixed tables). Note you can also have nested tables if you wish to group things e.g.

local tbl = {
  stats = {
    wins = 10,
   .....
  },
 Coins = 10
}
1 Like

What do you mean by, “non mixed” tables?

In Lua a table has an array part and a hash part and you can mix them e.g.

local a = {1, ['1'] = 2}

-- using both array and has part. This table will not save
print(a[1], a['1']) -- prints 1, 2

-- the below will save
local b = {1,2,3} -- this only uses the array part of the table
local c = {a = 1, b = 2, c = 3} -- this only uses the hash part of the table

2 Likes