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

No, don’t worry about implementation details. Just know that it separates the player.

What do you mean by “Separates the Player”?

local dataStore = DataStore2("key", player)

…will always get the data for that player. You don’t have to worry about it getting some other player’s data or anything, which is the reason you’d do .. player.UserId in normal data stores.

1 Like

Yeah I understand that, of course, but my concern was about understanding the way it looks like. Key is child of-whatever ways module implements creating DataStore for my player- that table and I can create as many keys as I want and all of them will be within players boundaries AKA same table.

If you specify the player, it’ll be “within players boundaries”. If you use combined data stores, it’ll be the same table. If you don’t, it won’t but it’ll still be exclusive to that player.

Okay now I understand everything. So when saving dictionaries I should use combined data stores and not this?

Having an all encompassing user data store like what seems to be in that post is worse and less ergonomic than combined data stores, yes.

1 Like

Thank you for clarifying that to me.

1 Like

If I use combined data stores, I will still have to get all values i want to store separately and then Combine them, but if I do that in playerAdded then I will have to do it every time player joins, is that correct way of doing it? I will be able to get this combined data after that and read it for other purposes as ONE won’t I? What’s difference between combined data Stores and GetTable? Can I use GetTable to save Dictionaries instead of CombinedDataStores?

1 Like

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?