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

I love this thing, I agree. It makes datastores that much easier.

5 Likes

If you use DataStore2, please take this survey (and share it to your friends who use it as well) :slight_smile:

3 Likes

Very useful and clean and helpful will be using this in the future.

1 Like

I’m going to implement this into my project tomorrow. Is good. :+1:

4 Likes

When I was looking through your examples I was wondering which method I should be using and if there are any trade offs of each?

local CoinStore = DataStore2("coins", player) -- Ex: 500
local Inventory = DataStore2("inventory", player) -- Ex: {"Sword", "Torch, "Pick axe"}

or

local DataStore = DataStore2("data", player) 
--[[ Ex: {
     Coins = 500; 
     Inventory = {"Sword", "Torch, "Pick axe"};
}

Thanks!

1 Like

The latter is better for avoiding throttling and is what combined data stores do automatically. I recommend almost exclusively using combined data stores for that exact reason.

DataStore2.Combine("data", "coins", "inventory")

-- ...

local CoinStore = DataStore2("coins", player) -- Ex: 500
local Inventory = DataStore2("inventory", player) -- Ex: {"Sword", "Torch, "Pick axe"}

-- both CoinStore and Inventory save and get from data meaning
-- there's less throttling while also keeping sane code
7 Likes

Ah, alright. I wasn’t aware of the DataStor2.Combine()… Thank you!

3 Likes

I do not understand datastore2 keys that well can the key be anything? With normal data stores the key is a string. I am planning on implementing DataStore2

1 Like

I’ve consistently used alphanumeric with underscores with no problems (A-Z, a-z, 0-9, _). I don’t know the exact limitations, but that seems like a more than reasonable set of characters. You should be using string keys.

1 Like

I think that you have misunderstood my question. For example in Nezuo’s post he has this DataStore2("inventory", player) isn’t player the key or am I missing something?

1 Like

No, inventory is the key.

I swapped them my bad. This is what confuses me

DataStore2 DataStore2(dataStoreName, player)
Example usage: local coinStore = DataStore2(“coins”, player)

Is that for player instances only?

Yes. DataStore2 is entirely based on players.

2 Likes

Hi, I am using DataStore2 in my game and I finally got accepted into the dev forum. :slight_smile: One question is regarding Combined data stores. If I am already using separate data stores, can I combine them now without losing any existing data?

For example (just using the same code from above), if my code is currently like this:

local CoinStore = DataStore2("coins", player) -- Ex: 500
local Inventory = DataStore2("inventory", player) -- Ex: {"Sword", "Torch, "Pick axe"}

People are already playing my game and have data. Can I now add the following line somewhere:

DataStore2.Combine("data", "coins", "inventory")

And in existing code sprinkled throughout the game, still use code without changing it (so it still looks like this):

local CoinStore = DataStore2("coins", player) -- Ex: 500
local Inventory = DataStore2("inventory", player) -- Ex: {"Sword", "Torch, "Pick axe"}

I think you said this is true, but I want to make sure before I do anything.

If yes, where/when should the Combine() function be called? Can I add it to any server script (that doesn’t have any delays)? Does it need to be called before any other call to Get/Set/Save data or is it order independent? Thanks!

6 Likes

Do you have plans of adding support for non-player related stuff so that you can take advantage of the automatic retry for things like settings for reserved servers?

1 Like

No, you cannot, because of how combined data stores work. You would have to write a wrapper on your own.

4 Likes

I don’t have any plans at the moment for it, although PRs are always open.

6 Likes

If I use this module, is there a need to set up my own autosave system that calls DataStore2:Save() every x minutes in case the Roblox datastores have an outage causing players to lose progress since they joined my game, or have you not found that this is necessary?

1 Like

Roblox data stores go down seemingly every weekend, so I wouldn’t rely entirely on DataStore2’s save on player leave feature. That being said, DataStore2 does not do this by default and it’s a non-goal of the project to provide that functionality.

If you’re going to auto save and you use combined data stores (like you should), know you only need to call :Save() on one data store under the key.

3 Likes

You might want to update that statement as requiring via ID is being removed.

5 Likes