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

Still doesn’t work. Might just try recoding it all to see if it fixes ;p

6 Likes

Can you add documentation on how to use ODS with this? Or give me the basic run down on how I would do it. Thanks.

5 Likes

You wouldn’t be able to because of how DataStore2 saves internally, apologies.

6 Likes

Ahhh thanks for the clarification I re-read the documentation multiple times thinking I missed something. So would it be alright if I used a separate ODS alongside Datastore2 or would the rates be hit?

5 Likes

As long as you’re smart about using 1 DataStore2 key (or use combined data stores), no.

8 Likes

Alright thanks for the clarification! Also really loving Datastore2

7 Likes

Is it possible to create a leaderboard with DataStore2? Like in games that shows the top 10 "insert action here"ers ?

5 Likes

As I said before, DataStore2 has no functionality like ordered data stores. It’s best to just use normal ordered data stores for that, sorry.

6 Likes

Rip, might leave a leaderboard out then. Thanks for the fast reply.

3 Likes

If you aren’t doing too many data store calls, you can just read players’ data from DataStore2, and save it to a regular ordered data store.

5 Likes

Been using this on a few of my projects, and I have to say it works great. Never had any issues in the few months of development I’ve been doing. Good stuff!

6 Likes

I’m having a small issue here. I’m trying to check to see if there is a cached value and if not it tells the client to do stuff.

Code below

DataStore2.Combine("MasterKey", "Avatar")
	
	local Avatar = DataStore2("Avatar", plr)



	if not Avatar:Get({},true)  then
	print('Player has no avatar data')
	game:GetService('ReplicatedStorage').Remotes.RemoteEvents.CustomizeAvatar:FireClient(plr,"New")

	end

help is appreciated

4 Likes

Is this really what you want? If you don’t provide a default to Get, it’ll return nil which may be what you want. I don’t see why you’d need to check specifically to see if there’s a cached value in the first place.

3 Likes

To check if its the player’s first time playing by checking if the player has data.

1 Like

Then just check if the result of :Get() is nil.

2 Likes

thanks

3 Likes

Added a little note at the beginning:

2 Likes

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!

13 Likes

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.

1 Like

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?