How would I check which player has the most coins?

#1

Hello! I’m trying to make a bar on the top of the ever player’s screen that says which player has the most coins in the server. Ex. “Richest person in this server: name”
I’m using IntValues in the the player folder to keep the player’s coins data. It can be accessed by: “game.Players.LocalPlayer.coins.Value” Any help would be very appreciated! :smile:

#2

Loop through all players and compare their ‘coins’ value.

local function GetRichestPlayer()
    local players = game.Players:GetPlayers()
    local most_coins, richest_player = -1, nil
    for i=1,#players do
        local player = players[i]
        local player_coins = player:FindFirstChild("coins") and player.coins.Value or nil
        if player_coins and player_coins > most_coins then
            most_coins = player_coins
            richest_player = player
        end
    end
    
    return most_coins, richest_player
end

2 Likes
#3

Thank you so much! :smile: :smile:

#4

You probably would want to get the current players inside the function instead of defining them once; the list won’t update when someone new joins.

2 Likes
#5

That was a typo, i added the function afterwards. I definitely meant to have it inside the function call. :smiley:

1 Like