Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to handle datastore failures?

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

✓ Accepted answer

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.

2 comments

0

Thank you, can you please PM me on roblox or twitter?

0

Contacted you via roblox DM

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.