Datastore failing to save SOMETIMES

Hi,

I have been having some issues with datastores for the past month. The datastore is functional and saving data for the most part however there are some players where it fails to retrieve data (I think) and sets their value back to 0.

I am not exactly sure why it is failing since I am unable to replicate the issue as it is player specific (e.g specs, internet connection, etc) however I will post my script so you guys can analyze it and see if there are any issues.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Saver = DataStoreService:GetOrderedDataStore("MilesLeaderstats")
local counterCoroutine = 0

Players.PlayerAdded:Connect(function(player)	
	
	local success1, err = pcall(function()
		coroutine.wrap(function()
		--	--print('started running coroutine')
		local success2
		local err
		
			while true do
				local ls = player:WaitForChild("leaderstats")
				local miles = ls:FindFirstChild("Miles").Value
			--	--print('going on anwyay')
				
				wait(300)
				
				local function saveDataSetAsync()
					success2, err = pcall(function()
						Saver:SetAsync(player.UserId, miles)
						--error("test error")
					end)
				end
				
				saveDataSetAsync()
				print('fired func')
				
				if not success2 then
					warn(err)
					wait(1)
					counterCoroutine += 1
					saveDataSetAsync()
					if counterCoroutine >= 3 then
						player:Kick("Failed to autosave miles data, this is likely a ROBLOX issue. Please report this in #bug-reports\nERROR CODE: BLUE-WATERMELON")
					end
				else
					warn("Autosaved successfully!")
				end
			end
		end)()
	end)
	
	if not success1 then
		warn(err)
	end
	
	----print('running normie script')
	----print(player.UserId)
	local attemptsToSave = 0
	local Data = nil
	
	local getDataCounter = 0
	
	local getSuccess
	local getErrorMessage
	
	local function getPlayerData()
		getSuccess, getErrorMessage = pcall(function()
			Data = Saver:GetAsync(player.UserId)
		end)
	end
	
	getPlayerData()
	
	if not getSuccess then
		print('failed!')
		
		warn(err)
		wait(1)
		getDataCounter += 1
		getPlayerData()
		if getDataCounter >= 3 then
			player:Kick("Failed to retrieve miles data, this is likely a ROBLOX issue. Please report this in #bug-reports\nERROR-CODE: RED-CABLE")
		end
	end

	if getSuccess then
		print('success')
		if Data then
			player:WaitForChild("leaderstats"):FindFirstChild("Miles").Value = Data
		else
		--	--print("No data!")
			player:WaitForChild("leaderstats"):FindFirstChild("Miles").Value = 0
		end
	end
end)



local function Save(player)
	local ls = player:WaitForChild("leaderstats")
	local miles = ls:FindFirstChild("Miles").Value
	local counter = 0
	
	repeat
		wait()
		local success, err = pcall(function()
			Saver:SetAsync(player.UserId, miles)
		end)
		if not success then
			counter += 1
		end
	until success or counter >= 3
	
	if counter >= 3 then
		counter = 0
	end
end

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

Thanks in advance

1 Like

First of all i highly recommend removing loops inside of those functions, this is not optimised at all and you might even exceed the data store limit per minute.

And as I said on the other post, use

dataStore:UpdateAsync(function(plr.UserId)
return dataorwhatever
end)

Because it is way more secure than :SetAsync

Is my datastore error handling good or does it need to be improved?