Best data saving technique?

What are your guys’ best ways to save data?

Im auto saving the player’s data every 5 minutes and still seem to be going over the limit on 14 player servers. Can’t figure out the reason why data loss is happening, so i’m asking you guys. Any tips and tricks?

3 Likes

I’m assuming you are using DataStore?

The DataStore limits should essentially reset every minute, so that means you are probably doing something wrong.

Are you retrieving the data properly? What method are you using to save? Is the output actually telling you that you are over the limit?

Feel free to provide code so that the above questions might be answered more efficiently :wink:

P.S. this topic should probably belong in #development-support:scripting-support

1 Like

Go-to #1 solution: don’t autosave every x amount of time. Load player data once when they join, and save it once when they leave.

3 Likes

Pro-tip - There are three different times you should be saving your data per player:

  1. Auto-saving every x seconds (frequency depends on your game; usually a few minutes is fine)
  2. When the player leaves
  3. When the game closes

Edit: And as @Rerumu was getting at, only load data when you have to. Usually player data only needs to be loaded once when they enter the game. Then just keep local changes, and periodically save them.

18 Likes

I would add that you should also aim to save immediately after any developer product purchase in order to prevent the possibility of loss.

12 Likes

I don’t agree with this being a #1 solution, there is the off chance possibility that datastores unexpectedly go down for some time and players leave assuming it’ll save.

(it has happened in the past btw)

2 Likes

And if that doesn’t work, a custom fallback can reassure that.

1 Like

That’s not really ideal for a situation when datastores go down for an extended period of time. You’re going to lose data, better to autosave prior in which it does happen.

You don’t want someone’s 1-2 hour play session to disappear just because of a load and save on leave only option? Roblox’s current datastore limitations most definitely have the number of requests to allow autosaving every few minutes.

Which is autosaving.

This is totally fine as long as you add more than just one saving method (leaving). A manual save button (cooldowned), robux purchases or some sort of major achievements are other types as well. Roblox even recently added a way to check the number of requests you have so that you can avoid stalling the service.

1 Like

From past experience, I’ve never experienced any supposed data loss regarding datastores or even going over the request limit (and this goes back to 2015 - 2016 when I had projects actively using them).
And, a custom fallback could be keeping a buffer somewhere in a table until the request can be fulfilled. I’ve never personally found an use for autosaving besides using up requests.
Ideally, if you are going to use them, you shouldn’t use them alone, and maybe just as a fallback, as proposed.

That said, the obvious problem being with x amount of players ingame making y requests for z time for autosaving, depending on what your variables are, can easily stall the service (although this was more relevant back when datastore limits were lower, which the wiki should probably update).

image

1 Like

I’ve never experienced issues with systems that involve loading when you join and saving when you leave. Just use that sort of system

I would say you should absolutely be autosaving.

I have personally experienced data loss in @Quenty’s Whatever Floats Your Boat when there was no autosave. Servers would crash and I would lose data and it was sad. He was doing what you suggest (saving only on exit), and it did not work well. He has since implemented autosave, from what I can tell, and it’s all good now.

Autosaving should take up maybe two or three requests maximum, and that’s if you’re doing something complicated (like keeping all save history in an ordered data store). If you save once every 5 minutes then you should have no problem with # of requests regarding saves. In less datastore-heavy games, you can even save once every minute and still be completely fine.


You must be using datastores way more often than you think. Maybe you got a number wrong (e.g. 5 instead of 5*60). You might also be using multiple keys/values per player. You only need to save to one key per player. Stick all of your data in one table so you only use one request when you save.


Here is what you should not do:

Data store error -> assume default data -> save default data


Here is what you should do:

Data store error -> use default data, but never save it -> retry datastore until it doesn’t error -> Only save if their normal data loaded


If you’re losing data, I assume you’re doing the first one, which means you probably aren’t doing any error checking. Sometimes data stores stop working, but your players’ save data is still there, just inaccessible. You assume (incorrectly) that the player has no save data and overwrite it with the default data – causing data loss.

You need to be checking datastore calls for errors using pcall. For example:

local data
local success, err = pcall(function()
    data = datastore:GetAsync(tostring(player.UserId))
end)
if success == false then
    warn("DataStore Error! Error: "..err)
    -- Use default data and mark it as "do not save ever"
    -- Retry data store every 30 or 60 seconds until it doesn't error
    -- Once it doesn't error, give the player their normal data and clear the "do not save ever" flag
else
   -- You have their data in `data`
   -- Do whatever you need to with it
end
7 Likes

I actually don’t autosave, my game only saves when you level up or when you leave. It’s just my saving solution is more stable.

I think you have to be very careful with autosave.

3 Likes

It makes sense to save on major events. Those are the most important things to save.

Can you confirm that you used to save only on leave? I remember losing levels a few times when servers would crash. It would be nice to know if that was the reason, and it would be a good example of why some sort of autosave – whether time based or on major events (level ups, purchases, etc.) – is important.

1 Like

Just wanted to chime in. Automatic datastore retries have been delayed due to higher priorities taking place. I’ll get back to it once priorities realign. :]

This 100%. Be very careful to avoid this pattern.

4 Likes

The way I save data is simply using PlayerAdded and PlayerRemoving, this seems to work the best for me because I don’t have to handle with complex data and saves me the hassle of taking requests. Here is a short example:

local DataStore = game:GetService(“DataStoreService”):GetDataStore(“Data”)
local Table = {}

– // Loading data when a player is added.

game:GetService(“Players”).PlayerAdded:Connect(function(Plr)
local DKey = “Key” … Plr.UserId
local Data = DS:GetAsync(DKey) or {
– // Add your datastore values here, EG: Cash = 100 (Change 100 to your starting amount)
},
Table[Plr]
end)

– // Saving data when player is removed.

game:GetService(“Players”).PlayerRemoving:Connect(function(Plr)
local DKey = “Key” … Plr.UserId
local Data = DataTable[Plr]
DS:SetAsync(DKey, Data)
end)

I hope this helped you out with your DataStore!

3 Likes

I’ve always only saved on level up and leave. The difference is using gameclose hooks vs. not.

6 Likes

This is a year old thread.

4 Likes