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

This seems to be more of a basic misunderstanding of how to code in Lua rather than an issue with the library, and as such, probably should be posted elsewhere.

2 Likes

Would DataStore2 be used more as a “backup” for data rather than the main data store? I remember someone (ForeverHD?) saying something about using it as a backup.

DataStore2 can be used as your main DataStore, but the difference between them is that DataStore2 does not overwrite the previous data, but saves the data on a different scope, allowing you to be able to restore data from past sessions.

2 Likes

Are we supposed to combine the DataStore in PlayerAdded or just once outside any functions?

Just once works, as long as it’s called before you actually use the data store. I like to put them at the beginning of the script.

1 Like

Hi @Kampfkarren.

I am having some problems with saving a Dictionary in DataStore2.

Could you please look at this post:
How to save Dictionary using DataStore2? - #7 by Oficcer_F

Thank you very much, I would really appreciate it if you would take time to help me.

I imagene many other people have issues with this also.

Sincerely.

Since there is essentially no data loss with this, is it still necessary for me to do :SetBackup()?

2 Likes

DataStore2 uses Roblox data stores internally. Roblox data stores go down. Yes.

3 Likes

How should I implement :SetBack()? Am I just able to throw :SetBackup(5) in my script and the module will take care of it? Or are there more steps that I need to do to utilize it properly? I don’t fully understand this part so I’m not entirely sure what to do.

2 Likes

If you do :SetBackup(5), then if the data store fails to retrieve itself 5 times it will just use whatever the default value you gave it is. Data will then not save so nothing gets overwritten.

As a developer, you need to make sure the player knows they’re on a backup (with :IsBackup()). Some developers choose to let players continue playing while telling them their data won’t save, others choose to simply kick players. It’s up to you what you do, as long as you do something.

3 Likes

So if a player were to join and their data didn’t load due to an error on Roblox’s end, I could do :SetBackup(5) and then they would not be playing on data that would be saved when they leave? Then if I were to have a script that would kick the player if :IsBack() returned true, and then they rejoined, they would be back on their data that was there before they joined and the data failure occurred. Am I understanding this correctly?

If you call :SetBackup and DataStore2 can’t load their data, it won’t save at all. You wouldn’t want it too–you don’t have their data.

3 Likes

So SetBackup() is like a safe guard against saving default data onto a player whose data wouldn’t load? And then when they rejoin, their data will be back to before the data failure occured? Sorry for asking so many questions, I just wanna make sure I am understanding this correctly.

1 Like

What you said is what backups are for, yes.

3 Likes

Sorry I have one more question, if I am using DataStore2.Combine, do I still use :SetBackup() for each individual DataStore? Or is there something else I have to do?

Also, would I place the :SetBack() before or after the .Combine? Or does it not matter?

You should only have to call :SetBackup once, but there’s no harm in doing it multiple times.

3 Likes

There’s no built in way to get previous data saves (besides the most recent one), right? Would i have to edit the module for that?

No, somewhat intentionally. The data loss prevention is so that setting/getting does not have data loss attached to it, not for something if you screw up. Also there’s no great way to write a generic API for it. You would have to modify the module.

1 Like

I see. I always like to have the option to revert data available for my admins, but i’ll re-evaluate how needed that is and try editing the module if necessary.

2 Likes

That’s a fair use case, I think. There may or may not be an issue on the GitHub page for this but if there isn’t please make one.

1 Like