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

Are you using the most recent version from GitHub?

As stated in the documentation, using the main key anywhere outside of DataStore2.Combine is undefined behavior, and shouldn’t work.

Yes I am, I have just started to use DataStore2

@Kampfkarren Hello I wanna make a global leaderboard where top 10 players will be shown. So I need to get my data ordered from high to low. Can I do that in datastore2?

No. He already told it. (30 chars)

So moving data from datastore2 to normal data store… How do I retrieve the saved value to implement on normal datastore?

I don’t know. I started studying about DataStore2 few hours ago.

okay thanks for info. I start digging more

If you handled your data stores as just using the user ID as the key and nothing else, then you can switch the saving method to Standard as explained in the documentation. Otherwise, you will have to manually retrieve it.

You didn’t tell me the error, and I do not have the time to read all the code you posted, sorry.

1 Like

You’re not providing anything to the default value of :Get(), so when you index it on a player with no data, it returns nil.

You need to provide a default value to get, in your case it could just be CreateDatastoreTable().MainOBJValues.

Does OnUpdate() repeat and repeat if yes then ill make a solution of saving tables

also last question can tables be saved using OnUpdate()

@Kampfkarren

:OnUpdate expects a function to be passed to it. The function is then called whenever the data store value updates. Kind of like listening for an event. You don’t use it to save things.

also btw no need to @ mention the op of a post since they already get notified of a reply

1 Like

We are trying to save the session play-time value to a players store when they leave, we are using PlayerRemoving:Connect but its not saving, my guess is that it is saving AFTER Datastore2 does its final save.

I noticed a BindToClose method but cant find any documentation on how it is used. Am I on the right track?

From the sound of it, you should just use :AfterSave.

Hey thanks for responding. I checked out :AfterSave and I am curious how to use this given our case.

We are trying to get the os.time when a player leaves the game, do some quick math, and then save their total play time. Our function fires with the PlayerRemoving:Connect method.

At this point, it seems like Datastore2 is doing its final save BEFORE our method gets a chance to fire, so ours fires off AFTER Datastore2 and the new data is only being cached and not saved before the player quits.

I got things to work by forcing it with Datastore2:Save() but i realize this is not ideal and redundant, it does indeed give me a warnign in Studio output that the request was queued and to reduce requests amount.

Note that all of our testing is in Studio.

I looked over AfterSave: and it seems to call back a function After we save to Roblox store. Since the save in question would be Datastore2’s final save for the player as they leave? how will an additional function be able to save it all again? Isnt this redundant?

It seems like when I need to do is be sure my fucntion bound to PlayerLeaving gets a chance to fire BEFORE Datastore2 fires its final save on the same event. I dont see a method in the Datasotre2 API that can do that, is there one?

Am I understanding the issue correctly?

Thanks for your help!

I misread. If I am understanding right, you want to store a value for the last time they played when they leave. You could utilize BeforeSave with this. Create a data store for this key, set it when they join (so that it’s different and will be saved), then have BeforeSave return the new value.

I couldn’t find this exact question here so I am here to see if there is an answer.

If you are accessing the same datastore from multiples scripts do I have to do a .Combine in both scripts?

Context:
I am working on a game with a currency and a daily rewards datastore but they are each different scripts. I was wondering if I just had to do this or what (in the daily rewards script):

local Data = datastore2("Currency", plr)
local currency = Data:Get()

It’s safest to combine in every script you use the key in, but you just need to guarantee that the data store will be combined before you use it.

2 Likes

Does DataStore2 need pcall? Also do I have to create another data store to transfer data between places?

(Sorry if these question sound stupid).