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


After reading through the implementation, I have a few concerns.

  • Save() does not wrap ‘SetAsync’ in a pcall, so any errors as a result of self.dataStore:SetAsync, or self.orderedDataStore:SetAsync will cause a leak in the DataStoreCache for ‘playerLeaving’, and will also break out of the autosave while loop, disabling auto-save for this player for the remainder of their session.

  • It doesn’t seem like self.beforeInitialGet should be a list - instead it should just be a single registered callback, as currently, only the result of the last iterated callback in self.beforeInitialGet list will be assigned to self.value. Similarly, only the result of the last callback in self.beforeSave will be used as the saved value in DataStore:Save(). Since the order of table iteration is undefined, this is problematic.

  • Most of the useful error/critical messages are only logged via ‘warn’. On a large game with many servers these reports will go almost entirely unseen. It would be useful to add an API for the user to provide an error callback that would be invoked in these cases so that the user could log these errors (to the Analytics/Logging platform of choice).

Let me know if something in my comments is a result of misunderstanding. Other than that, nice work on the module!


I’ll look into this. I’m fine with :Save() tripping off errors but the PlayerRemoving should wrap it in a pcall.

The auto save code doesn’t work even and is going to be removed.

Good point. Made an issue for it.

Also a good idea, made an issue for it.

Thanks for looking through my code.


One question, how would I go about changing a player’s stats when the player isn’t currently present.
Looking through this, it appears I need a player instance to access the datastore2. Represented here:

local buckStore = DataStore2("BuckStats", plr)

Is there a way I can access the data with just a username or a userId?


DataStore2 does not support cross-server communication.


:+1: Thanks! Not that I needed it, but helpful to know.


If you were going to ‘edit’ any of the values you created for the datastore, (such as ‘points’) in a different script, would you need to require the module, and use :Increment() to add to the value? Or could we simply just use math?


I’m not sure what you’re asking? The values are the same across different scripts if you use :Get().


I mean would I have to do:

randomvalue.value = randomvalue.value + 5

or use:

my question is, do I have do use :Increment() in a different script besides the Datastore2, or would I still have to require() the datastore, in the new script I’m using and then use :Increment()? Or could I simply do it the traditional way of using math to add?


:Increment sets the value, just grabbing the value and adding won’t set it. If you need to set the value, use :Increment, otherwise traditionally add.


Well after watching AlvinBlox’s tutorial and explanation, he states,
“You can use :Set() to set the value, like, :Set(500) will set to 500 every time, if you use :Increment(500) it’ll add 500 instead of setting the value to 500”


Yes, like in the thread:


I am making the model a package so that it’s easier for developers to update their copies and eliminates one of the reasons to require by ID. Hopefully everyone who is requiring by ID can still do so, but please tell me if this messes up anything!



Great project but please implement cross-server communication. Most developers don’t really need to do anything other than reading/getting data from the server, for me anyways I handle all data setting on the server all I do is just fetch data from the client to update UI and etc, I know I could use :OnUpdate and keep sending info to the client and the client can update UI but having it built-in would be very nice.


Do you mean network communication and not cross server communication?


Mhm, I misworded it.


I suppose a feature like that would be neat, but not something I’d prioritize right now. :OnUpdate and a RemoteEvent seem like the best way to do it.


Well thanks for the quick response, also a system for easily loading backups onto main datastores would be neat, I haven’t look at much of your code but I don’t believe it is currently possible. Would be useful for if by chance data loss did happen and you needed to load a backup.


Scratch this, apparently it didn’t save as a package at all??