How can I safely and consistently apply changes to a Datastore2 setup?

Hi!

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 :/.

About to add in a couple of new dictionary keys with values that are table. Currently my datastore2 is set up in a similar way to this:

	Ints = {
		["Zombucks"] = 75;
		["ScientistWins"] = 0;
		["JudgeWins"] = 0;
		["SurvivorWins"] = 0;
		["Level"] = 0;
		["XP"] = 0;
		["Donation"] = 0;
		["Version"] = 3;
	};
	VialSkins = {
		["VialDefault"] = true;
		["VialRainbow"] = false;
		["VialMagma"] = false;
	};

	Preset = {
		["GunSkins"] = "GunDefault";
		["VialSkins"] = "VialDefault";
		["Characters"] = "CharTom";
	};
	
	Claims = {
		["FirstTime"] = true;
		["MasterPack"] = false;
		["BoomBox"] = false;
	}

Except obviously with more key/value pairs and tables. Is this a good way of setting up my datastore, or should I set it up differently?

If I should set it up differently, what would be a good method to migrating people who’ve been using this setup?

Have you added a pcall function?

This has nothing to do with a pcall function at all.

Oh, sorry! I misread the script.

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.

1 Like