Datastore Pages & Leaderboards

Working on leaderboards for my game, I have over 400k+ unique keys so I’m trying to find the best way to get them all sorted and displayed on the top 50 leaderboards.

My issue is I’m getting zero errors in the console, and it only prints an empty table so yeah. :man_shrugging:

local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetOrderedDataStore("PlayerData")

local compiledData = {}
local loading = false
local timeUntilReset = 10
local MaxDisplayRank = 50
local SortBy = "Loyalism"
-- Options: Token, Loyalism, Robux

if SortBy == "Loyalism" then
	script.Parent.Parent.Title.Text = "MOST LOYALIST"
	script.Parent.Parent.Value.Text = "LOYALISM"
elseif SortBy == "Token" then
	script.Parent.Parent.Title.Text = "RICHEST PLAYERS"
	script.Parent.Parent.Value.Text = "TOKENS"
elseif SortBy == "Robux" then
	script.Parent.Parent.Title.Text = "TOP DONTATORS"
	script.Parent.Parent.Value.Text = "ROBUX"
end

local function Sort(Data1, Data2)
	if SortBy == "Loyalism" then
		return Data1[3] > Data2[3]
	elseif SortBy == "Robux" then
		return Data1[6] > Data2[6]
	elseif SortBy == "Token" then
		return Data1[4] > Data2[4]
	end

	return Data1[3] > Data2[3]
end

while wait(1) do
	if loading then
		script.Parent.Parent.ResetTime.Text = "loading"
		
		return true
	end
	
	timeUntilReset = timeUntilReset - 1

	script.Parent.Parent.ResetTime.Text = "Resets in " .. timeUntilReset .. "s"

	if timeUntilReset == 0 then
		loading = true
		compiledData = {}
		timeUntilReset = 60

		for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		task.wait()

		local listSuccess, pages = pcall(function()
			return PlayerData:GetSortedAsync(false, 100)
		end)

		if not listSuccess then return warn("failed to get pages") end
		
		local finishedLoadingData = false
		
		repeat
			local data = pages:GetCurrentPage()

			for _, v in ipairs(data) do
				local plrdata = v.value -- PlayerData:GetAsync(v.KeyName)
				warn(plrdata)
				local split = string.split(v.key, "_")
				local selectedCharacter = nil

				if plrdata.Data.SelectedCharacter then
					for _, character in pairs(plrdata.Data.Characters) do
						if character.FullName == plrdata.Data.SelectedCharacter then
							selectedCharacter = character;
						end
					end
				end

				if selectedCharacter ~= nil then
					local Tokens = 0;
					local Pockets = {}

					if selectedCharacter.InventoryItems.Pockets ~= nil then
						if selectedCharacter.InventoryItems.Pockets.Items ~= nil then
							Pockets = selectedCharacter.InventoryItems.Pockets.Items;
						end
					end

					if SortBy == "Token" then -- save proccessing time
						for _, Item in pairs(Pockets) do
							if Item.Type == "Token" then
								local json = HttpService:JSONDecode(Item.Data)

								if json.Amount ~= nil then
									Tokens += json.Amount
								end
							end
						end
					end

					table.insert(compiledData, {
						Username = Players:GetNameFromUserIdAsync(tonumber(split[2])),
						UserId = split[2],
						Loyalism = selectedCharacter.Stats.Loyalism or 0 ,
						TotalTokens = Tokens,
						Inventory = {},
						TotalDonated = plrdata.Data.TotalRobuxDonated or 0
					})
				end
			end

			if pages.IsFinished then
				finishedLoadingData = true
			else
				pages:AdvanceToNextPageAsync()
			end
		until finishedLoadingData == true

		table.sort(compiledData, Sort)

		print(compiledData)
		
		for Rank, Data in ipairs(compiledData) do
			if Rank > MaxDisplayRank then return false end

			local Value = nil;

			if SortBy == "Loyalism" then
				Value = addSuffix(Data[3])
			elseif SortBy == "Token" then
				Value = addSuffix(Data[4])
			elseif SortBy == "Robux" then
				Value = addSuffix(Data[6])
			else 
				Value = 0
			end

			local template = script.Template:Clone()
			template.Name = tostring(Rank)
			template.PlrName.Text = Data.Username
			template.Rank.Text = "#" .. tostring(Rank)
			template.Value.Text = Value
			template.Parent = script.Parent	
		end
		
		loading = false
	end
end