How to make an IntValue's value in ascending order according to another value's descending order?

I’m sorry for the topic title not making any sense but I will further explain what I mean.

So I’m trying to make a leaderboard GUI where it shows the player stats. It includes the rank number, the player name, and the number of wins. I am trying to make the rank’s IntValue according to the player’s wins.


Rank Wins
1 12
2 7
3 2

So my problem is in setting the rank value according to its comparison to the others. I already took 3 hours trying to find any solutions and trying to make my own solution but I can’t solve it.

Here is my code

-- Server Script

	local folderChildren = game.ReplicatedStorage.PlayerRanking.PlayerWins:GetChildren()
	local folderChildrenCount = #folderChildren

	local RankNumber = game.ReplicatedStorage.PlayerRanking.PlayerWins:WaitForChild("Player"..folderChildrenCount).Rank.Value
	local playerWins = player.leaderstats.Wins.Value
	local playerLevel = player.leaderstats.Level.Value

	local function UpdateLeaderboard()
		local RankNumber = 1
		for i,v in pairs(game.Players:GetChildren()) do
			local otherPlayerWins = v.leaderstats.Wins.Value
			if playerWins < otherPlayerWins then
				RankNumber += 1



The problem with this code is that it only changes the rank of players that join, if a new player joined and is higher than the old player


Before someone joins:
Rank Name Wins
1 OldDude 20

After someone joins:
Rank Name Wins
1 NewDude 50
1 OldDude 20

This is another code that I found from a post

local function rankUpdate()
	local folderChildren = game.ReplicatedStorage.PlayerRanking.PlayerWins:GetChildren()
	local folderChildrenCount = #folderChildren

	local p1 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player1
	local p2 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player2
	local p3 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player3
	local p4 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player4
	local p5 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player5
	local p6 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player6
	local p7 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player7
	local p8 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player8
	local p9 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player9
	local p10 = game.ReplicatedStorage.PlayerRanking.PlayerWins.Player10
	local values = { 
		{p1, p1.Wins.Value},
		{p2, p2.Wins.Value},
		{p3, p3.Wins.Value},
		function(a,b)--Sorts values
			return a[2]>b[2]

	for i,v in pairs(values) do
		-- how do I set the rank values in order?

As you can see I have no idea what it is and how I can set the rank values with it. I am sorry if the post explanation is kind of confusing or if I am wasting your time if this is a very simple thing to do but I am just a beginner scripter. Please, I would REALLY appreciate any help. Thanks!

Also, this is saved in DataStore, if that will help in answering the post…

Couldn’t you just use ipairs? It will iterate the table in order from least to greatest index.

1 Like