Why won't my datastore saving system retry properly after failure

local success, errormessage = pcall(function()
		DataStore:SetAsync(Player)
	end)
	
	if not success then
		local Retries = 0
		repeat
			local worked = pcall(function()
				DataStore:SetAsync(Player)
			end)
			Retries += 1
warn(Retries)
			task.wait(1)
		until
		worked or Retries >= 5
		print(Retries)
	end

This is my code, basically I’ve purposely made it fail so that I can see how it retries, I used to make it retry with a task.wait(.1) but I thought that was too fast because you can only send 1 setasync/getasync call per second according to the equation that they give you with 1 player.

The problem is it only retries once instead of 5 times. When i switch it to task.wait(0.1) it does all the times its required to. It doesn’t throttle but i’m worried it might in a live game if its task.wait(0.1)

1 Like

You seem to be defining worked inside the repeat block, instead of defining it outside and setting it from within
Also… the limit is 60 + nPlayers * 10 per MINUTE

yeah so thats 70 for one player right per minute?

so because i got my maths degree in harvard university i’m able to calculate how many calls u can send per sec:

70/60 = 1 (nearest whole number)

so u can only send one per sec

if we wanted to be more exact, u can send 0 per 0.1 seconds according to my math.

The limit is per minute, not per second

Wait so you can do anything per second?

But the script identifies the worked var since it’s within the scope, so why should I make it outside?

Oh yes, nevermind, I’m dumb, in repeat loops, all the variables declared inside can be used for the “until”
I’m actually not sure, the repeat loop shouldn’t really quit until either retries is 5 or the pcall works, have you tried printing worked to see if it somehow worked?

i’ll try printing worked but it really shouldn’t because im setting the key as an instance. usually when i make task.wait(1) it only retries once and then it never does again, whereas with tsak.wait(0.1) it retries all 5 times.

is there anything wrong in my code other than the setting of the key?

just printed worked under the pcall and its false + it only retries once when the tsak.wait is 1 second.

local success, errormessage = pcall(function()
		return DataStore:SetAsync(Player)
	end)

You should use return before DataStore:SetAsync(Player).

Oh alright i’ll try this thanks

Wait a minute is this jsut a tip though? Is this not an actual fix to the problem? Also why would you need to return setasync because it doesn’t really return anything useful.

You may as well go straight into the retry loop, usually it’s a good idea to slow down the request rate too to avoid the accumulation of multiple players failing at the same time then hitting the rate limits.

local retries = 0
repeat
     local success, errormessage = pcall(function()
          return DataStore:SetAsync(Player)
     end)
     if not success then
           retries += 1
           warn(retries)
           task.wait(retries)
    end
until success or Retries >= 5
--I would also add:
if retries == 5 then
    warn(Player.Name, "data not saved")
    --additional handling/warning etc
end

Oh wait I sae it returns the errormessage, but that wouldn’t fix the problem right?

I’ll try this out thanks for the help.

You should use return before both calls to DataStore:SetAsync(Player). Can you reply with your updated code and a screenshot of the error?

The warning message is “argument 2 missing or nil” and I just tried out @Wigglyaa’s method but it still only prints 1 retry.

Players.PlayerRemoving:Connect(function(Player)
	local SaveData = {
		Time = Player.leaderstats.Time.Value,
		RobuxDon = Player.leaderstats.RobuxDon.Value
	}
	
	local retries = 0
	repeat
		local success, errormessage = pcall(function()
			return DataStore:SetAsync(Player)
		end)
		if not success then
			retries += 1
			warn(retries)
			task.wait(retries)
		end
	until success or retries >= 5
	--I would also add:
	if retries == 5 then
		warn(Player.Name, "data not saved")
		--additional handling/warning etc
	end
end)
1 Like

The thing is though I don’t want it to work, because I want to test the retry system. my data saving system already works i’m just testing my retry system.

And the problem with my retry system is that it only retries once instead of 5 times if I make the delay longer. If I make the task.wait one second it will only retry once for some reason.

Maybe it’s due to the fact the games shutting down?