How to script a Global Leaderboard

Alright, you got Discord? I believe it’s easier to communicate there.

Yes :slightly_smiling_face::slightly_smiling_face:

Add me, Tor#1329. Reply with your username so I know who to add (got 49 pending)

I’m nammed Léolol DB… so it will be a little bit obvious :slight_smile:

Is the Global leaderboard a free model? Some leaderboards only work with some type of a object, if it is the team create helper that made it, tell him or you to recheck the scripts.

As mentioned, you can change the w value. This is were the data is sourced from. It does not require playerPoints.

local w = plr.leaderstats.Cash.Value

Pulls player’s leaderstat cash value. plr is the player object.

More explanation: How to change where value is sourced from.

1 Like

For a leaderboard like that you are going to need 3 different items:

  1. Rank
  2. Username
  3. Points

Now we are going to break this down 1 by 1 on how we get each item

But first what you need to do is setup a basic DataStore to save the player’s points
Personally how I would do it is like this:

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("PointsStore")

Now that we have a basic DataStore setup we can begin to use it, now for saving a player’s data, I like to load their data whenever they join the game and then save it whenever they leave, which is nice for saving bandwith. Here is an example of the code I would use.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local Points = PointsStore:GetAsync(Player.UserId)
	--Then you could set the in game value to the saved value, such as a number value
end)

Players.PlayerRemoved:Connect(function(Player)
	local Points = --This will equal the player's current points
	local Success, Err = pcall(function()
		PointsStore:SetAsync(Player.UserId, Points)
	end)
end)

Now that we have a basic storage system setup to save the player’s points we can now look into converting it into a leaderboard.
For the leaderboard we are going to be using Roblox’s built in leaderboard tool called OrderedDataStores

You can have another script setup here that handles the leaderboard

local Players = game:GetService("Players")

local PageSize = 10 --This is how many player's data will be on every page
local UpdateDelay = 60 --This is how often the leaderboard will update, minimum is 60 seconds
while true do
	local SortedStore = PointsStore:GetSortedAsync(false, PageSize)
	local Page = SortedStore:GetCurrentPage()
	for i,v in pairs(Page) do
		local Username = Players:GetUserIdFromNameAsync(v.key)
		local UserId = v.key
		local Place = I
		local Value = v.value
		--With this information you can now construct your Gui
	end
	wait(UpdateDelay)
end
3 Likes