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

Love the module, it makes life generally so much easier.
Other than the well known “no offline access” issue which should probably be builtin at some point, my only problem with the module is that it does not seem to work properly when executed from the command bar.
After thorough testing, requiring and getting cached data works from all server scripts, but always fails to return the cached data from the command bar, instead returning the datastore value. This happens when the Datastore2 module is accessed from the command bar directly, or by another module that is accessed by the command bar.
This is not critical because it only affects Studio testing, but it is something you should look at, if even fixable.

1 Like

I’m not sure what the use case for using the command bar with DataStore2 is.

1 Like

One would be manually running a module script that uses DS2 from the command bar and perhaps print it’s output to test it’s functionality.

1 Like

So not sure exactly what the problem is or if this is even a good way to use the :Set() function.

So basically I made a few Values for Kills, Wins, Losses etc. I then made a .Changed function on all of them and inside those .Changed Functions it does the :Set() function. Now when I change my values using the Console it changes and saves fine but whenever the game changes the values. They change but don’t save? This has really confused me.

Has anyone else experienced this?

1 Like

I’m not sure what you’re referring to, though the way you should structure your code is that everything that changes stats just calls Set through the data stores, and the leaderstats just use OnUpdate on the data store. You shouldn’t be modifying the leaderstats directly.

1 Like

How I could create a global leaderboard? since Datastore2 doesn’t have ordered datastores?

Any solutions for it? or at least plans to implement a ordered datastores into Datastore2?

1 Like

You will have to create your own OrderedDataStore

1 Like

I’ve used DataStore2 for multiple projects but haven’t used .Combine(). What exactly does this do and why should I use it?

2 Likes

You absolutely MUST do it. I didn’t really fully understand it until I looked at the code for myself:

That should give you a more clear indication as to what it does.

EDIT: If you’re still unclear, it basically combines all keys into a single DataStore, this will help avoid the rate-limits that are imposed by Roblox.

2 Likes

So do I combine all data keys I use?

1 Like

So do I combine all data keys I use?

Yes, that is correct.

2 Likes

Alright thanks,
A few more questions.

When using this do I need to change what I do with :Get()
Do I need to call this more than once in a script (ex. everytime a player joins)

2 Likes

No, Combine should be completely transparent. Use it once and forget about it. You only need to call it once, you just need to make sure you call Combine before using any method on that data store.

3 Likes

When using only one key it is not needed to combine the key (e.g. a dictionary)
This is only a MUST when you have multiple keys as multiple keys can throttle if not combined.

1 Like

I’d still do it regardless, even the official documentation example script uses .Combine while having only one key:

https://kampfkarren.github.io/Roblox/guide/basic_usage/

2 Likes

If you’re only ever storing one key and it’s a dictionary, you’re just doing the work Combine does but by hand and not dedicated.

You should use Combine even if you have one key that’s just a dictionary since you never know when you’ll add more.

2 Likes

So basically I’m working on a car game and I’ve been able to implement DataStore2 beautifully. The only problem is if I want to add another car it won’t work for existing players. So essentially, I have all the cars saved in a table with more values inside of it, like color, wheel type, whether the player owns the car or not etc, and all I need is to make another one of that table, meaning a new car is added.

Here is the code I use:

-- (variables etc) --
local userDataStoreName = "Test3"
DataStore2.Combine(userDataStoreName, "tables", "cash", "laps")

local userData = {
	Cars = {
		["CarType"] = {
			["Car1"] = { -- existing car
				["Cost"] = 1000;
				["Owns?"] = true;
				["Rims"] = "Rim1";
				["Paint"] = {255, 255, 255};
				["Reflectance"] = 0.15;
				["Wrap"] = "0";
			};
			["NewCar2"] = { -- new car
				["Cost"] = 1000;
				["Owns?"] = false;
				["Rims"] = "Rim1";
				["Paint"] = {255, 255, 255};
				["Reflectance"] = 0.15;
				["Wrap"] = "0";
			};
		};
	}
}

So the Car1 is an existing car that the player owns. But in a new update, for example, I add a new car, NewCar2, won’t be added to the existing player’s data for some reason. Is there a way to add something to the dictionary without resetting the data?

If it’s more than one layer deep like that, you can just do it manually.

When using a for i, v loop to increment the DS. Do I get: Datastore first? or go straight into the increment.

I have a question, im trying to convert datastore to datastore2. So i need to save a value, but i only need its name as its a tag. And most datastore2 only save the values, and the name is chosen by the script