How to save data even if datastore gives external server error

rarely, when I try to save data to datastore HTTP-smth error appears and doesnt save the data
How do I counter this and save the data anyway, its gonna be annoying if thats happens to a player and they lose their data from this session

theres code: (very basic)


local DataStoreService = game:GetService("DataStoreService")
local UpgradesSave = DataStoreService:GetDataStore("Upgrades")
local CurrencySave = DataStoreService:GetDataStore("Currency")

local function serializeUpgrades(folder)
	local data = {}
	for _, item in folder:GetChildren() do
		data[item.Name] = item.Value
	end
	return data
end

game:BindToClose(function()
	local ownerId = game.ReplicatedStorage.PSOwnerID.Value
	if ownerId ~= 0 and game.ReplicatedStorage.DataLoaded.Value then
		
		pcall(function()
			local upgradesData = serializeUpgrades(game.ReplicatedStorage.Upgrades)
			UpgradesSave:SetAsync(ownerId, upgradesData)
		end)
		
		pcall(function()
			local currenciesData = serializeUpgrades(game.ReplicatedStorage.Currencies)
			CurrencySave:SetAsync(ownerId, currenciesData)
		end)
		
	end
end)

What is the error message that’s shown?

it was something like HTTP-idk: external server error

You can try to save the data again if you fail the first time

For example

local success, fail
local attempts = 1

repeat
success, fail = pcall(function()
Datastore:SetAysnc(player.UserId,DataToSave)
end)
if not success then
print(fail)
attempts += 1
task.wait(3)
end
until success or attempts == 5  
if success then
print("Successful")
end

Success returns a bool of if it worked or not
fail returns a error if it failed but if you do

success. fail = pcall(function()
return Datastore:GetAysnc(player.UserId,DataToSave)
end)

fail or any other names you set it to will be equal to whatever your return if success is true and worked.

this example is not in how your script would look bc I am writing this on mobile.

I hoped this worked for your problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.