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

Added a “Gotchas” section for common mistakes you may come across when using DataStore2.

58 Likes

This. :revolving_hearts:
Is. :two_hearts:
Beautiful. :sparkling_heart:

EDIT: In the future, making this so that you don’t have to have a player instance, but just a datastore key would also be incredibly helpful!

54 Likes

The problem with just a data store key is that DataStore2 only saves on leave, so it’s not guaranteed the data you’re trying to get will be the same. What’s the use case for non-player instances? I can add it if I feel the use case is valid.

50 Likes

I’m definitely aware that there aren’t as many uses for a regular dafastore key, however, some games may use a global weather systems, notifications, etc. However, with DataStore2, would these systems really require this complexity… :thinking:

Either way, amazing system. Just integrated it into my game and the speed is definitely noticeable!

39 Likes

I think for the use cases you provided the benefits of DataStore2 would not be applicable or noticable, you’re much better suited using standard data stores or HttpService.

Glad to hear DataStore2 is working for you, can you message me a link to your game so I can use it as a success story? :slight_smile:

37 Likes

:AfterSave was added a little bit ago. In order to fix a minor memory leak, I am going to inform all users to remember the rule of not using DataStore2 in PlayerRemoving. This will now be enforced when this memory leak is fixed. Move everything you might put in PlayerRemoving for DataStore2 in AfterSave callbacks.

36 Likes

Suggested by yours truly

38 Likes

Added a backups section and three new APIs for backups in the case of Roblox outages.

33 Likes

That would help me too, I haven’t really used a Datastore 2 before :wink:

40 Likes

I can look into it @Aorda @Beartikal.

34 Likes

An issue with BeforeInitialGet has been spotted to where it is called on every :Get(), rather than just the first time. If you are currently using BeforeInitialGet, be prepared for this to change.

29 Likes

Added :GetTable, a method that makes it much easier to add new keys to tables in your data store.

26 Likes

Video with information on how to use it and do more complex saving like whole inventory tables, not just one coin number would be awesome :smiley:

36 Likes

I’m not a fan of video tutorials and I’m not under the right conditions to record a video at this point in time, sorry :frowning:

29 Likes

Been trying to set this up, but I feel like a lot of the documentation is really vague. The biggest issue is I have no idea how to obtain previously saved data. Does the Get() function grab a key? A value of something? Really confusing.

For me personally, having the functions written like this is also extremely frustrating:

Variant DataStore2:Get(defaultValue=nil, dontAttemptGet=false)

It’s really hard to understand what the parameters mean. The “variant” and “=nil” stuff makes it difficult for me to figure out the arguments I should use. I get you’re trying to be informative but can you make it slightly more readable?

And the descriptions aren’t that helpful. Is defaultValue supposed to be the key of the thing I want?

Also your example code isn’t helpful that much. Why are you using the comments to explain stuff totally unrelated to the module (e.g. “–Make sure the player is buying a real product”)? I’m confused on what exactly you’re giving the player when they join too… Are you getting them a value 100 or setting them or what? Very hard to understand.

I really would like to use a stable saving system like this but it’s confusing to set up. I hope you can address that stuff.

36 Likes

The key is defined when you use DataStore2(dataStoreName, player), dataStoreName being the key (it’s different to the key that’s used internally, but that doesn’t matter for a user).

The :Get() function will grab the value of the key you defined when you used the DataStore2 function. defaultValue is the value you want :Get() to return and save if the value in the data store is nil. Variant just means that it returns a Variant, AKA any data type.

I gave comments on the example code about unrelated things so that people don’t conflict what is and isn’t DataStore2.

I’m not sure how I’d improve the documentation, what would you suggest?

27 Likes

Thanks for letting me know. I think just putting information like that into the documentation would be a lot more helpful. It would be incredible if your example showed how you retrieved data because (from what I can tell) it doesn’t. And more comments explaining what you’re doing with DataStore2.

As for the functions in the documentation, I just personally dislike people putting things in there that you wouldn’t necessarily use in the script. I’m not sure how to explain it, but basically people putting stuff in there besides what I should be using just makes it harder for me to read.

Anyway, I can tell you put a lot of work into this. I appreciate your quick answer to my questions as well

22 Likes

Both my examples retrieve data through :Get(), are you referring to something else?

20 Likes

Ok, so I was originally confused by the “100” in the Get() function in the example. I didn’t realize that was the default value, so I guess it was really my fault for not noticing. I think you could still benefit from leaving a comment there explaining that you’re getting the data, and if it isn’t there, setting the default value to 100.

This is where your documentation gets confusing:

Variant DataStore2:Get(defaultValue=nil, dontAttemptGet=false)
Example usage: coinStore:Get(0)
If there is no cached value, it will attempt to get the value in the data store. Otherwise, it’ll return the cached value. If whatever value it gets is nil, it will return the defaultValue passed. If dontAttemptGet is true, then it will return nil if there is no cached value.

I think it could be worded better. Simply changing the first sentences to something like “The module will attempt to get the value in the data store. If the data is cached, the cached data will be returned instead” would probably be beneficial since it explains what the function does clearly, and then explains that it could also be cached. (If that’s even how it works) You could also explain what the arguments are and what they do bit better too. Something as simple as “If no data is found, defaultValue will be returned” would help me.

Again, I can tell you spent a lot of time on this so don’t worry about changing the documentation or whatever if you think it’s fine. I’m just having some OCD. I’m going to just experiment with it and see what happens. And thanks for making this thing free too!

26 Likes

Added Fairy World to the list of games with millions of visits using that I am aware of that use DataStore2 (3 million visits).

21 Likes