How should I make leaderboard that would check if players have more points than others

  1. What do you want to achieve? I want to make a script that would check the player points if it is more than others.

  2. What is the issue? The issue is that I can’t figure how should I do it

  3. What solutions have you tried so far? I tried to find solutions in dev hub, youtube, devforums. But I could not find.

It’s an example how the leaderboard works like arsenal once.

maybe just use math.max to see who has the most points

2 Likes

Well. Let me try that later if it works for me.

Store the values in an array and compare values , for example-

local t = {100,123,5,34,63,84} ;

local Max = 0 ;
for i , v in pairs(t) do
    if(v > Max) 
        Max = v ; 
    end 
end

This is of linear time complexity.

You can use table.sort to sort an array containing every player and their points into a descending order, that way you can retrieve the first, second and third player by just using their position(for example the first player is players[1]):

local Players = game:GetService("Players") 

--convert all the players and their points into an array
function getPlayersArray()
	local players = {} 
	for _, p in pairs(Players:GetPlayers()) do 
		local points = p:FindFirstChild("Points") --change this to the points location 
		table.insert(players, {player = p, Points = points})
	end
	return players 
end

local players = getPlayersArray()

--sort the array(that's why we used arrays instead of dictionaries)
table.sort(players, function(a, b)
	return a.Points > b.Points 
end)

--print the array in the sorted order
for _, v in pairs(players) do 
	print(v.player, v.Points)
end

local first = players[1] --since the array is sorted, the first player has the highest points 
local second = players[2]
local third = players[3]
--etc. 

print(first.player, first.Points) 
4 Likes