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

Oh, right. My bad. I don’t have SaveAll in my version, I forgot it’s part of the newer versions. I can still use d2.Save() or ds2.SaveAsync() correct?

You’d use :Save(), but please instead just get the free model and manually apply the new code by copying and pasting it from GitHub. Requiring by ID is not at all recommended–I do intend to push code that would create backwards incompatibility with code not prepared for it.

1 Like

Why is it not recommended to use require
To get DataStore2?

So instead of requiring by ID it’s better to require the module directly from serverstorage (or whatever place you’ve put the model) to prevent any backwards incompatability when code is pushed.

But is always safe to just copy paste the code directly from github (link) when the code gets updated?

If you use require, then whenever I push a breaking change (a change that is backwards incompatible), you won’t have the time to prepare and your code could immediately break instead of you transitioning over.

1 Like

The recommended way is copying and pasting the code from GitHub (come to think of it, I could release a .rbxmx file you could use as well). Keep track of updates I give on the news thread to see if I’m going to release anything backwards incompatiblem I’ll maek a warning.

3 Likes

Don’t push breaking changes then.

:joy:, Okay thanks for telling me. :slight_smile:

1 Like

Hi! :smiley:
What’d be the best way to save table data to the datastore?

1 Like

As answered several times in this thread, just like in normal data stores there’s no special way to save tables in DataStore2–just use :Set.

2 Likes

I got it, but now I get this error:

Invalid at input.VO.0 because: Array with non-sequential indexes
Invalid data while saving

What does it mean? Is there a fix?

notes: I’m saving a table which has int values and other tables which have strings as indices and contain ints

1 Like

Looks like when I remove the tables that are in the table it saves fine. So it’s impossible to save a table inside a table? How would I replace that then?

1 Like

Incorrect.

Just like data stores, you cannot store tables with numeric keys that aren’t a table (ordered keys). Meaning you probably have a structure that looks like { [100] = "foo", [623] = "bar" } etc. You can save your keys as strings instead for it to work–this is an issue with data stores that DataStore2 carries over.

3 Likes

My structure looks like

{
   ['Coins'] = 0,
   ['Inv'] = {['Tool1'] = 0, ['Tool2'] = 1},
   ['ToolOwned'] = {[0] = true} -- If the tool is owned its tool ID will be here set to true
   ['Rebirths'] = 0
}

The issue is with the elements like Inv and ToolOwned, which are tables. When I have them in, I get the error I showed in one of my previous messages. When I remove them, it’s fine.
How do I avoid the error but still have those tables in?

EDIT I read your post, I didn’t understand how it solves the problem though unfortunately :frowning: Maybe give an example?
Thanks in advance by the way :smiley:

1 Like

Read my post.

Your keys aren’t ordered, and yet are numeric, thus they cannot be saved normally. Save the keys as strings.

2 Likes

Now I understand, I’ll try it!

EDIT It worked! Thank you so much!
Again, thanks for your help and the awesome module. It will really help me out. Have a great day! :smile:

2 Likes

Hi, I am having trouble with Incrementing table values. If I have

local playerData = Datastore2(“MainKey”,player):Get()

This contains 2 tables. In one of the tables I have a key/value pair - PlayerHunger = 0

I have been able to set the value by using…

playerData:Get().PlayerHunger = 25

However I cannot work out how to Increment this value (using :Increment() ). Any help would be much appreciated.

1 Like

No you haven’t. You need to explicitly call :Set in order to set it.

You cannot call :Increment on a data store that’s not a number. You should be using combined data stores, which would let you increment specific keys.

By the way, my newly released game uses DataStore2, and I love it. No reports of data loss (except for before I updated to the newest version before shutting down servers) yet. You can add Liftoff to your list. It’s made us for more Robux than I could’ve imagined for a game that’s only been out for a few days, with a solid robux/visit ratio of 2.6.

2 Likes

@Kampfkarren Can Datastore 2 store dictionaries… say for items within an inventory with different stats inside of them? And if not, what would be the best way to store a large array of items, aswell as all the data that belongs inside them, such as: Item count, Item Name, Item Strength, Item protection, Item color(Customized details and such), as well as perhaps the best way to save a characters appearance through items they are wearing in an rpg setting. Would that be done through values? The player has values within them that are are linked to certain items? I don’t know if that last part made sense but really my biggest thing are the items for now.

As stated several times before in this thread, just like normal data stores DataStore2 can store dictionaries, tables, etc.

2 Likes