Showing multiple pieces of data on a Global Leaderboard using one DataStore

So I’ve been trying to make a global leaderboard. But the problem is that all my data is saved in a table using one DataStore. As far as I searched, there are no tutorials or posts that show anything about how to solve my problem. The format of the table is shown as below:

-- What is saved in the DataStore
local data = {
	cash = player.leaderstats.cash.Value,
	level = player.leaderstats.level.Value,
	cash = player.leaderstats.cash.Value,
}

-- Saving the data when the player leaves
dataStore:SetAsync(player.UserId, data)

My code so far:

local module = {}

local replicatedStorage = game:GetService("ReplicatedStorage")
local dataStoreService = game:GetService("DataStoreService")
local serverStorage = game:GetService("ServerStorage")
local _workspace = game:GetService("Workspace")
local players = game:GetService("Players")

local dataHandler = require(serverStorage.modules.dataHandler)
local playerData = dataStoreService:GetOrderedDataStore("playerData")

module.reloadBoard = function()
	pcall(function()
		for _, board in pairs(_workspace.objects.start.leaderboards:GetChildren()) do
			local pages = playerData:GetSortedAsync(false, 10)
			local list = pages:GetCurrentPage()

			for rank, data in ipairs(list) do
				local template = script.template:Clone()
				template.rankText = string.format("#%s", rank)
				template.name.Text = data.key
				template.amount.Text = data.value
			end
		end
	end)
end

task.defer(function()
	while true do
		for _, player in pairs(players:GetPlayers()) do
			dataHandler.saveSessionData(player)
		end

		for _, obj in pairs(_workspace.objects.start.leaderboards:GetDescendants()) do
			if obj.Parent.Name ~= "ScrollingFrame" then continue end
			if obj:IsA("UIListLayout") then continue end
			obj:Destroy()
		end

		module.reloadBoard()
                wait(15)
	end
end)

return module

Once I play the game, pretty much nothing updates, no errors in the output.

Anything would be appreciated

1 Like

Use GetOrderedDataStore() to create as its name suggests an ordered DataStore, then use GetSortedAsync() to parse through its entries, check the DataStore documentation page for more information.

1 Like