My players are setup using Datastore2 to save integer data, their characters, skins… All that kind of stuff. They’re all set up into individual datastores and combined together nicely. This works perfectly when setting up the game, but now I’m making some changes (changing some characters to free, adding new characters etc etc) how can I consistently make changes to everybody’s datastore2 when they rejoin?
I’ve taken the approach of adding a “Version” integer, and currently I have this sort of setup:
local vers = intData["Version"]
if vers == 1 or vers == 2 then
intData["Version"] = 3
local toAdd = 0
if charData["CharAhmed"] == true then
toAdd = toAdd+2000
end
if charData["CharNoob"] == true then
toAdd = toAdd+2000
end
intData["Zombucks"] = intData["Zombucks"] + toAdd
ds2("Ints", player):Set(intData)
charData["CharAhmed"] = true
charData["CharNoob"] = true
ds2("Characters", player):Set(charData)
end
This does not look or feel sustainable. What happens after 20 version changes, do I just need to make code to accomodate every single version potentially rejoining? This will be a massive bloat.
I can do this, but I don’t want to. Has anybody got any other suggestions as to how I can update my datastores consistently?
I’m going to be adding large changes to my DataStore2, adding two whole new dictionary tables. I would really appreciate some insight on how I should properly handle adding additional data - I don’t know any best practice to approach this :/.
If you are only updating tables, then you can use :GetTableAsync or :GetTable, these as specified in the DS2 API
Will get the value (either from the cache or Roblox data stores), and patch it with the default value if it doesn’t have the keys.
Basically if that key does not exist in the players stored data but does in the default value, it will then add that key that is not currently saved to the data that is returned. You can then use :Set to update the data that is saved.
And @heysticks no Data Store 2 methods need to be wrapped in a pcall because there is no chance if set up correctly of them erroring.