Try Sending Fewer Requests. Data Store =

I have this warning that occurs only in game and not in studio. I have two ordered data stores and only the rebirths one is erroring.

My code snippet is below. When I comment out the cache function this error does not occur. Any ideas?

local function cacheLeaderboard(dataStore, stat)
	local isSuccessful, pages = pcall(function ()
		return dataStore:GetSortedAsync(IS_ASCENDING, PAGE_SIZE)
	end)

	if not (isSuccessful) or not (pages) then
		warn("[LeaderboardHandling]: Leaderboard could not be cached.");
		return;
	end
	
	local count = 0;
	local validEntries = 0;
	
	while (true) do
		for i, entry in ipairs(pages:GetCurrentPage()) do
			count += 1;
			if (count > MAX_ENTRIES) then
				break;
			end

			local country = string.sub(entry.key, 1, 2);
			local userId = tonumber(string.sub(entry.key, 3));
			local player = MethodUtil.getPlayerFromUserId(userId);

			if (player) then
				local playerObject = Player.players[player];
				if (playerObject) and not (country == playerObject.country) then
					local isSuccessful, errorMessage = pcall(function()
						dataStore:RemoveAsync(entry.key);
					end)
					
					if not (isSuccessful) then
						warn(errorMessage);
					end
					continue;
				end
			end

			local value = math.ceil(MethodUtil.decompressNumber(entry.value, SCALE_FACTOR));	
			national[country] = national[country] or {};
			national[country][stat] = national[country][stat] or {};
			international[stat] = international[stat] or {};

			table.insert(national[country][stat], {
				userId = userId, 
				value = value,
				country = country
			});

			table.insert(international[stat], {
				userId = userId, 
				value = value,
				country = country
			});

			if (validEntries < LEADERBOARD_ENTRY_COUNT) then
				topNational[country] = topNational[country] or {};
				topNational[country][stat] = topNational[country][stat] or {};
				topInternational[stat] = topInternational[stat] or {};

				table.insert(topNational[country][stat], {
					userId = userId, 
					value = value,
					country = country
				})

				table.insert(topInternational[stat], {
					userId = userId, 
					value = value,
					country = country
				});

				validEntries += 1;
			end
		end
		
		if not (pages.IsFinished) then
			local isSuccessful, errorMessage = pcall(function()
				pages:AdvanceToNextPageAsync();		
			end)
			
			if not (isSuccessful) then
				warn(errorMessage);
				break;
			end
		else 
			break;
		end
	end
end
Update.Event:Connect(function()
	table.clear(international);
	table.clear(topInternational);
	table.clear(national);
	table.clear(topNational);
	
	for i, player in ipairs(Players:GetPlayers()) do
		local playerObject = MethodUtil.conditionalTimeout(function()
			return Player.players[player];
		end)
		
		if not (playerObject) or (MethodUtil.getLength(playerObject.data) == 0) then
			warn("[LeaderboardHandling]: Player object was not found for " .. player.Name);
			continue;
		end
		
		local combinedTapMultiplier = 0;
		for i, petData in ipairs(playerObject.data.pets) do
			combinedTapMultiplier += Player.getPetMultiplier(petData).tapMultiplier;
		end
		
		local key = "";
		local isSuccessful, country = MethodUtil.getCountry(player);
		if (isSuccessful) and (playerObject.data.isCountryVisible) then
			playerObject.country = country;
			key = country .. player.UserId;
		else
			playerObject.country = "??";
			key = "??" .. player.UserId;
		end
		
		combinedTapMultiplier = math.ceil(MethodUtil.compressNumber(combinedTapMultiplier, SCALE_FACTOR));
		local isSuccessful, updatedValue = pcall(function(currentValue)
			return HighestTapsDataStore:UpdateAsync(key, combinedTapMultiplier);
		end)
		
		local rebirths = math.ceil(MethodUtil.compressNumber(playerObject.data.rebirths, SCALE_FACTOR));
		local isSuccessful, updateValue = pcall(function(currentValue)
			return HighestRebirthsDataStore:UpdateAsync(key, rebirths);
		end)
	end
	
	cacheLeaderboard(HighestTapsDataStore, "highestTaps");
	cacheLeaderboard(HighestRebirthsDataStore, "highestRebirths");
	LeaderboardsUpdated:FireAllClients();
	
	wait(ConstUtil.LEADERBOARD_UPDATE_COOLDOWN);
	Update:Fire();
end)

No delay in loop?

add some delay in this loop

Does advancing to the next page cause data store issues if you call it repeatedly?

Unsure if this fixes it, but just wanted to point this out incase of script exhaustion.
At the start of the script, I added wait(), instead of using true.

while wait() do
		for i, entry in ipairs(pages:GetCurrentPage()) do
			count += 1;
			if (count > MAX_ENTRIES) then
				break;
			end

			local country = string.sub(entry.key, 1, 2);
			local userId = tonumber(string.sub(entry.key, 3));
			local player = MethodUtil.getPlayerFromUserId(userId);

			if (player) then
				local playerObject = Player.players[player];
				if (playerObject) and not (country == playerObject.country) then
					local isSuccessful, errorMessage = pcall(function()
						dataStore:RemoveAsync(entry.key);
					end)
					
					if not (isSuccessful) then
						warn(errorMessage);
					end
					continue;
				end
			end

			local value = math.ceil(MethodUtil.decompressNumber(entry.value, SCALE_FACTOR));	
			national[country] = national[country] or {};
			national[country][stat] = national[country][stat] or {};
			international[stat] = international[stat] or {};

			table.insert(national[country][stat], {
				userId = userId, 
				value = value,
				country = country
			});

			table.insert(international[stat], {
				userId = userId, 
				value = value,
				country = country
			});

			if (validEntries < LEADERBOARD_ENTRY_COUNT) then
				topNational[country] = topNational[country] or {};
				topNational[country][stat] = topNational[country][stat] or {};
				topInternational[stat] = topInternational[stat] or {};

				table.insert(topNational[country][stat], {
					userId = userId, 
					value = value,
					country = country
				})

				table.insert(topInternational[stat], {
					userId = userId, 
					value = value,
					country = country
				});

				validEntries += 1;
			end
		end