roblox how to make a global stats leaderboard code example

Example: global leaderboard roblox

local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("Points") 
 
local function printTopTenPlayers()
	local isAscending = false
	local pageSize = 10
	local pages = PointsODS:GetSortedAsync(isAscending, pageSize)
	local topTen = pages:GetCurrentPage()
 
	-- The data in 'topTen' is stored with the index being the index on the page
	-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
	for rank, data in ipairs(topTen) do
		local name = data.key
		local points = data.value
		print(data.key .. " is ranked #" .. rank .. " with " .. data.value .. "points")
	end
 
	-- Potentially load the next page...
	--pages:AdvanceToNextPageAsync()
end
 
-- Create some data
PointsODS:SetAsync("Alex", 55)
PointsODS:SetAsync("Charley", 32)
PointsODS:SetAsync("Sydney", 68)
 
-- Display the top ten players
printTopTenPlayers()

Tags:

Misc Example