How should I manage my global leaderboard data?

I set up a DataStore with data used for my global leaderboard and have come across an issue where there are too many entries in the DataStore to filter through them all as Roblox limits the rate at which you can use GetAsync().

Here’s what it looks like on the server (within a module script):

local DataStoreService = game:GetService("DataStoreService")
local rankedLeaderStore = DataStoreService:GetDataStore("RankedLeaders")
local leaderData = {}
local rankedLeaderDictionary = {}

RankedEvent.OnServerEvent:Connect(function(player, eventtye, equippedCards, rankedDivision, rankedElo)
		
		if eventtye == "addLeaderStats" then
			local key = player.UserId .. "1"
			local dataToStore = {
				KeyName = player.UserId,
				name = player.Name,
				cards = equippedCards,
				division = rankedDivision,
				elo = rankedElo
			}
			local success, errorMessage = pcall(function()
				table.insert(leaderData, dataToStore)
			end)
			if not success then
				print(errorMessage)
			end
		end
	end)

Players.PlayerRemoving:Connect(function(player: Player)
	for i, data in pairs(leaderData) do
		if data.KeyName == player.UserId then
			local key = player.UserId .. "1"
			rankedLeaderStore:SetAsync(key, data)
		end
	end
end)

function RankedServer.SetupLeaders()
	while true do
		local listSuccess, pages = pcall(function()
			return rankedLeaderStore:ListKeysAsync()
		end)

		if listSuccess then
			while true do
				local enemyInfo = pages:GetCurrentPage()
				for _, v in ipairs(enemyInfo) do
					local value = rankedLeaderStore:GetAsync(v.KeyName)
					rankedLeaderDictionary[v.KeyName] = value
				end
				if pages.IsFinished then
					break
				end
				pages:AdvanceToNextPageAsync()
			end
		end
		task.wait(600)
	end
end

In the SetupLeaders function, the enemyInfo table is saved/loaded the same way as the leaderData table, using a different DataStore, so I didn’t include it in the code provided.

Here are the errors that I get:

Is there a way to make this work with my current system or do I need to change how I save/load the data entirely?

1 Like

Unfortunately leaderboards are only possible through Roblox’s OrderedDataStores or a 3rd party database. You’ll need to change the way you’re storing and accessing the data.

For a leaderboard, you’ll need a separate ordered data store. They can only store one positive integer, meaning to have multiple leaderboards you’d need multiple ordered data stores. From there, you can use the built in method of an ordered datastore to get your ordered list.

The two main caveats of ordered data stores for leaderboards is that they’re slow and require a separate data store. I’d highly recommend exploring 3rd party data storing options like MongoDB or MySQL. There are lots of tutorials and examples on how to set them up for your project.

2 Likes

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