Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to handle datastore failures?

Asked by 6 years ago

So say this is my data that I would like to store, now I've read the info on the wikis about failures but since this doesn't auto save, how would I make sure a player gets notified that their data may not save? Should auto saving be implemented or not? And how would I make it so that if, in fact, the data may not save that it doesn't overwrite the players save file? Not understanding how to do this.

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
statz={"Kills","WOS","Noobs"}
game.Players.PlayerAdded:connect(function(plyr)
    local a=Instance.new("NumberValue")
    a.Parent=plyr
    a.Name="leaderstats"
    for i=1,#statz do
        local stat=Instance.new("NumberValue")
        stat.Parent=a
        stat.Value=0
        stat.Name=statz[i]
    end
    local child=plyr.leaderstats:GetChildren()
    for i=1, #child do
        child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
    end
end)

game.Players.PlayerRemoving:connect(function(plyr)
    local child=plyr.leaderstats:GetChildren()
    for i=1, #child do
        child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
    end
end)

2 answers

Log in to vote
0
Answered by
lukeb50 631 Moderation Voter
6 years ago
Edited 6 years ago

All datastore operations should be wrapped in a pcall. This prevents your script from breaking if an operation does fail and gives you the reason why it failed.

This would be one way to implement it:

success,message=pcall(function()
    --do datastore operation
end)

the success variable is a boolean that tells you if the operation completed and message is a string with an error message if success is false. Just remember that message is a not a user-friendly message and in most cases should not be used as text you send to the user.

from there you can send them a message in any way to tell them that saving failed. Autosaving can be a good feature as long as you don't do it too often.

more info here:http://wiki.roblox.com/index.php?title=Global_namespace/Basic_functions#pcall

If a datastore operation fails, no data is changed or corrupted. You can just try again.

0
Thank you, can you please PM me on roblox or twitter? greybird700 63 — 6y
0
Contacted you via roblox DM lukeb50 631 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

You should make a backup datastore named something like backupstats and keep your data there. Then, when you set new data, edit it into stats first, and if it fails, set the stats back up to backupstats. But, if the save is successful, save the stats into backupstats too.

Answer this question