502 error on AdvanceToNextPageAsync()

Hello Developer Forum. I have been at this issue for about a week and cannot figure it out. The function AdvanceToNextPageAsync() has been erroring crazy. I tried wrapping it in pcalls but now it takes forever for leaderboards to even initialize and it seems they wont even update after the first time anymore, I’m just left more confused than ever.

Note: this code runs perfectly fine in studio, but not in public servers, hope that info is helpful.

Code:

local sg = script.Parent
local sample = script:WaitForChild("Sample")
local sf = sg:WaitForChild("ScrollingFrame") 
local ui = sf:WaitForChild("UI")

local DataStoreService = game:GetService("DataStoreService")

local DataStoreName = "MainGameData"


local DataStore = DataStoreService:GetDataStore(DataStoreName)


local update = nil



local T = {"K","M","B","T","q","Q","s","S","O","N","d","U","D"}
local function formatNumber(n)
	if not tonumber(n) then return n end
	if n < 10000 then return math.floor(n) end
	local d = math.floor(math.log10(n)/3)*3
	local s = tostring(n/(10^d)):sub(1,5)
	return s.." "..tostring(T[math.floor(d/3)])
end

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("DonationLeaderboard")




wait(5)
update = true
while true do
	
	local Success, Pages = pcall(function()
		return DataStore:ListKeysAsync()
	end)

	if not Success then warn("Timeout") return end

	while update do
		local Items = Pages:GetCurrentPage()

		for _, Data in ipairs(Items) do
			local Key = Data.KeyName
			local value = DataStore:GetAsync(Key)
			local data=value or {}
			local sdaroc = data["important"]["Donations"]
			if sdaroc then
				pcall(function()
					dataStore:SetAsync(Key,sdaroc)		
				end)
			end
			task.wait()
		end

		if Pages.IsFinished then 
			update = false
			break end
		
		repeat
			local Success, Errormessage = pcall(function()
				Pages:AdvanceToNextPageAsync()
			end)
			if not Success then
				task.wait(1)
				print("did not advance page dono leaderbaord")
			end
		until Success
		
	end
	
	
	
	local smallestFirst = false
	local numberToShow = 100
	local minValue = 1
	local maxValue = 10e30
	local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
	local top = pages:GetCurrentPage()
	local data = {}

	for a,b in ipairs(top) do
		local userid = b.key
		local sadrocks = b.value
		local username = "[Failed To Load]"
		local s,e = pcall(function()
			username = game.Players:GetNameFromUserIdAsync(userid)
		end)
		if not s then
			warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		table.insert(data,{username,sadrocks, image})
	end
	ui.Parent = script
	sf:ClearAllChildren()
	ui.Parent = sf
	for number,d in pairs(data) do
		local name = d[1]
		local val = d[2]
		local image = d[3]
		local color = Color3.new(1,1,1)
		if number == 1 then
			color = Color3.fromRGB(255, 243, 79)
		elseif number == 2 then
			color = Color3.fromRGB(171, 209, 229)
		elseif number == 3 then
			color = Color3.fromRGB(255, 159, 103)
		end
		local new = sample:Clone()
		
		new.Image.Image = image
		new.LayoutOrder = number
		new.Rank.Text = "#".. number
		new.Rank.TextColor3 = color
		new.PName.Text = name .. " | "
		new.Value.Text = formatNumber(val)
		new.Value.TextColor3 = color
		new.PName.TextColor3 = color
		new.Parent = sf
	end
	wait()
	update = false
	wait(120)
	
end

repeat
local Success, Errormessage = pcall(function()
Pages:AdvanceToNextPageAsync()
end)
if not Success then
task.wait(1)
print(“did not advance page dono leaderboard”)
end
until Success

this is the main portion of code I believe is causing the issues.

EDIT: Tried the method recommended, there are no errors in console anymore but it just prints over and over "“did not advance page dono leaderboard”. Eventually after multiple minutes it will show but then it will never update it no matter how long I wait.

The only thing I can think of is, you’re hitting a rate limit of some kind at that point, out of curiousity, could you up that task.wait in that repeat loop you point out at the bottom to 5 seconds?

1 Like

From Data stores | Documentation - Roblox Creator Hub, this is what a 502 represents.

502 API Services rejected request with error X: Error X occurred when processing on Roblox servers. Depending on the response, you may want to retry the request at a later time.

1 Like

The same thing seemed to be happening in public servers. Now in studio it is even up to luck if it shows, I really do not know what could be going on here.

I tested it, i put prints in the success portion of code as well, on server startup there is a few successes then immediately cuts to failures for minutes on end. Really don’t know what to do from here.

hi there :3

when i made a leaderboard for my game in the past, i got a lot of errors aswell. the error was because roblox has a rate limit on getting datastore values and whatnot.

simply add a task.wait here and there and see if that resolves your issue.

2 Likes

Hello! I realized that my issue was my understanding of this request limit. I had three separate scripts on accident all saving different data and it was very inefficient so I put them in the games main data script and it solved everything, thanks for the help still!

2 Likes

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