My DataStore randomly failed?

I have over 100,000 visits on my game and today is the first time that my DataStore has failed. I’m a new developer and I tried for an hour to figure out what happened but I can’t figure out what caused this DataStore to fail. I joined one of my servers because I kept getting complaints that the server wasn’t acting right and some of the scripts weren’t working how they were supposed to so I shut down the server and one of the players that were in the server when it shutdown lost all of their leaderstats. Luckily the leaderstats were only there to tell the players how long they have played the game but still, this worries me because I’m planning on using the same DataStore for a new game that I’m working on. The in-game currency is going to be a big part of this new game because you will be able to buy the currency with Robux.

DataStore:

game.Players.PlayerRemoving:connect(function(player)
	local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Save")

local statstorage = player:FindFirstChild("leaderstats"):GetChildren()
for i =  1, #statstorage do
	datastore:SetAsync(statstorage[i].Name, statstorage[i].Value)
	print("saved data number "..i)
	
end
print("Stats successfully saved")	
end)

game.Players.PlayerAdded:connect(function(player)
		local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Save")
	
	player:WaitForChild("leaderstats")
	wait(1)
	local stats = player:FindFirstChild("leaderstats"):GetChildren()
	for i = 1, #stats do			
	stats[i].Value = datastore:GetAsync(stats[i].Name)
	print("stat numba "..i.." has been found")
		end
end)
2 Likes

You should try saving them with a table instead of individually, because there’s a limit on how many times you can call the datastores with setasync.
The more values you add the more issues you’ll get with the method you’re currently using.

4 Likes

Luckily there is an entry found here which provides the details of how to write a decent DataStore module, which includes features such as handling failures and auto-saving.

Like @kidso52 said, what you should be doing is storing ‘session data’ within a table which all changes are written to or read from during a live session, and then whenever a player leaves or an auto-save takes place, only then save the data to the DataStore. This will prevent hitting any DataStore limits.

4 Likes

I’d definitely agree with all the points stated in the posts above, you’ll need to handle failures using protected calls and you’ll need to save data in a BindToClose function on shutdowns.

I wrote a thread talking about using pcalls, I hope it’ll help:

5 Likes