How Would I Determine Which Player Has The Most Currency?

So I am trying to figure out how I would basically find out which player in a server would have the most currency in a game. I tried looking through global leaderboards for help, but I can’t quite seem to find any answers. Please give me any good things out there that will help me with this, or just explain it in really good detail, so I can understand it. My plan is that when I find the player with the most currency in the server, they will be given a crown to wear and it will be taken away from them if they lose that top position on the leaderboard.

Thanks For Reading! :grin:

4 Likes

Using table.sort on Players:GetPlayers() could be an option. If your currency is somewhere in the player, use this method.

In the table.sort function, a and b are player instances. Just put where the currency is and it should work

local function GetRichestPlayer()
   local Players = game.Players:GetPlayers()
   table.sort(Players, function(a,b)
          return a.CURRENCYNAME.Value > b.CURRENCYNAME.Value
   end)
   return Players[1]
end

local RichestPlayer = GetRichestPlayer()
7 Likes
function getMax(stat)
	local player = nil
	local value = -math.huge
	local temp
	for _, p in ipairs(game.Players:GetPlayers()) do
		temp = p.leaderstats[stat].Value
		if (temp > value) then
			player = p
			value = temp
		end
	end
	return player
end

local richestPlayer = getMax("Money")
if (richestPlayer) then print(richestPlayer.Name) end
3 Likes