I'm developing my own game but datastores seem to be an issue. I originally had a game with a few players, and my datastores throttled a lot even though I only saved once every 60 seconds and when a player leaves. The problem is I have dev products that give e.g cash, and their purchase is technically worth nothing if their data is lost. Is there any fool-proof methods of recovering data or preventing data loss, including if Roblox has problems with datastores failing globally?
What are the best ways to prevent data loss?
1 answer
The way's I normally handle it is I make a function to save the values and run it whenever a player leaves If the server is said to shut down, And lastly every 60 seconds. Here are some examples of what I mean.
local function saveValues(player)
-- CODE GOES HERE
end
game:BindToClose(function()
if not #game.Players:GetPlayers() == 1 then -- Don't save twice if its only 1 person leaving.
for i, player in pairs(game.Players:GetPlayers()) do
saveValues(player) -- Call save function
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveValues(player)
end)
while wait(60) do
for i, player in pairs(game.Players:GetPlayers()) do
saveValues(player)
end
end
I would also like to mention that It can be worth running the function to save values if you get an important stat, for example, you get a legendary item in the game. Or if you just rebirthed.
4 comments
Line 6 will not work as you expect for it to. not #game.Players:GetPlayers() will be false, since the not is applied to the number, which is then saying if false == 1 then, which will never execute. if not (#game.Players:GetPlayers() == 1) then is what you're looking for, but to make it simple just do if #game.Players:GetPlayers() > 1 then.
This is not how you want to special case the last player anyways. Datastore writes are asynchronous, so you want to have a dictionary of all the players who are currently in the process of saving, and on BindToClose() initiate save for anyone not already saving, and don't return from BindToClose until the dictionary is empty.
It can still fail if not everyone saves in the 30 seconds BindToClose buys you, but you can't help that, there is no way to keep the server alive any longer. This is why you should definitely save immediately for important transactions or events, as this answer recommends.
I'm still curious how I would get around outages that apparently happen often where I wont be able to save or load data.
This discussion is preserved as it was. New answers, comments, and votes are closed.

6 comments
Have you tried DataStore2? https://devforum.roblox.com/t/how-to-use-datastore2-data-store-caching-and-data-loss-prevention/136317
I've seen that post before actually and it looks good but I'm a bit confused. Currently my game is almost complete, and I don't know if the post is saying I need to create the values within the module. Is it possible for me to use datastore2 to just save existing values without creating them?
I haven't touched datastore2 yet, I don't know. But you can experiment with it to see.
I will check it out later then, thanks! I'd still be interested in seeing if I could script a datastore script myself that is as close to efficiency as DataStore2
I'm intrested, im also doing Datastores for the first time. How many players did your original game have that you said was getting throttled a lot?
@ShinyGriffin I was at around 30 to 40 concurrent players at once on my game, and servers were 30 players each.