How to use DataStore2 - Data Store caching and data loss prevention

I saw that you mentioned it would be better to use combined data stores instead of one large datastore for saving a lot of different data. Is there any particular reason for this? Currently my game (which is not yet launched) uses one data store which saves a table that looks like this:

{
	currency = {
		coins = 0;
		gems = 0;
	};
	inventory = {
		['AK-47'] = true;
		['M1911'] = true;
		['Knife'] = true;
		['Starter Hood'] = true;
		['Starter Pants'] = true;
		['Starter Shirt'] = true;
		['No Hair'] = true;
		['No Hat'] = true;
	};
	crates = {
		['crateName'] = 1; --crateName = amountOwned
	};
	levels = {
		level = 1;
		xp = 0;
	};
	upgrades = {
		['gunName'] = {['upgrade1'] = 1;}; --upgradeName = upgradeLevel
	};
	equipped = {
		weapons = {
			primary = 'AK-47';
			secondary = 'M1911';
			melee = 'Knife';
		};
		clothing = {
			hats = 'Starter Hood';
			shirts = 'Starter Shirt';
			pants = 'Starter Pants';
			hair = 'No Hair';
		};
		skins = {
			primary = '';
			secondary = '';
			melee = '';
		};
	};
	skinTone = 'white';
}

Are there any reasons I should convert this to a combined data store before my game launches?

I had this problem a long time ago when one of my modules were stuck as “Not for sale”

What I did to fix it was to find a dead obtainable (or alternatively you can simply publish to roblox as another script) and then replace it with that one.

I believe the datastore automatically does this.
You don’t need to do anything on your end. Using multiple datastores should prevent throttling completely.

I’m not sure what you’re asking. To combine data stores, right after requiring DataStore2 write:

DataStore2.Combine("DATA", "coins", "levels", "whatever", "etc")

And then just use the stores as normal

DataStore2("coins", player)
DataStore2("levels", player)
DataStore2("whatever", player)

This question doesn’t make sense. They’re not comparable.

You can save dictionaries anyway with just normal :Set.

It’s more ergonomic to use (easier to read, but also lets you just call :Set, :Increment without having to get the table, change one value, then set), lets you do things like serialize/deserialize specific stores (to make things like your inventory and whatnot smaller to save), and in the future will be prioritized in deveopment and made the default.

If your data already looks like that, you can just make the combined key (the first argument to .Combine) whatever it is now.

I know how data stores work but I’ve never used them. Should I learn to use normal Roblox datastores before this, or is this something I can start off with?

Does combined datastores combine multiple datastores in one, or it just creates combined datastore. I mean should I :Get() all keys before I combine them? Also it seems like Get is not Setting cache value which in normal datastore it does(I’m talking about GetAsync which is intuitive equivalent to Get for someone new to DataStore2). So once I get I should also set? Why isn’t get also setting it?

Ok so this is my script and it errors on print(unpack(Wealth)) and says that it’s nil, what am I doing wrong?

-- Services
local DataStore = require(script.Parent.DataStore2) 
local Players = game:GetService("Players")
local Storage = game:GetService("ServerStorage")
local ReplicatedSorage = game:GetService("ReplicatedStorage")

Wealth = {["Coins"] = 60000, 
		  ["Gems"] = 0
		 }

Players.PlayerAdded:Connect(function(Plr)
	
	DataStore.Combine("Wealth", "Coins", "Gems")
	local WealthStore = DataStore("Wealth", Plr)
	
	local Wealth = WealthStore:Get("Wealth", Wealth)

	WealthStore:Set(Wealth)

	print(unpack(Wealth))
end)

You can start off with this, but it never hurts to learn something.

That’s not how combined data stores work.

I see that, then I have to do it like this?

Players.PlayerAdded:Connect(function(Plr)
    local WealthStore = DataStore("Wealth", Plr)
    local CoinStore = DataStore("Coins", Plr)
    local GemStore = DataStore("Gems", Plr)

    DataStore.Combine("Wealth", "Coins", "Gems")

    local Wealth = WealthStore:Get("Wealth", Wealth)

    WealthStore:Set(Wealth)

    print(unpack(Wealth))
end)

why do I need combined dataStores, if i need to manually Create all of them anyways?

No.

Put this after you require DataStore2, and put another key before all that.

DataStore2.Combine("DATA", "Wealth", "Coins", "Gems")

that’s what I did in previous reply, it’s just my master key is Wealth itself

I do it like you said but I get Set and Get part wrong

I see. Then just use Coins and Gems as you would normally, and never use the name “wealth” anywhere outside of the master key. Combined data stores are written such that you can just use the stores normally after you combine them.

But what’s the reason for using combined datastores then, If I can’t access many keys with one key?

Why would you want to get ALL the data a player has instead of just actually what you’re asking for?

So the way DataStore2 should be used is this:

1.You should Create DataStore for all data separately
2.Then you should Get values for each
3.Then you should set
4.Then every time you need data(For example to show money on display) you just :Get() it
5.Every time data changes you set it again

Tell me what I get wrong

Thank you very much! I believe this will be very helpful to many developers including myself!

Not after getting unless you actually want to change the data, but other than that yes.

CharacterAdded doesnt seem to work after using :Get, is this intended?
(or at the very least, it doesnt work when the player first spawns, it only works after player respawns)

1 Like