Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do i find out which player has the highest of a leaderstat?

Asked by
Cen_ix 0
5 years ago

So im trying to make a system where when a map changes, the game finds out which player has the highest of a leaderstat score. i know that math.max() will return the highest number in a set of numbers, but i would need to find out which player has that number, which again, not hard.

My problem being getting the numbers, there doesnt seem to be any easy ways to do this since math.max wont return the highest of a number if you were to do something like for _,numbers in pairs(tableHere) do print(math.max(numbers)) end, so how should i be doing this?

1 answer

Log in to vote
0
Answered by
RubenKan 3615 Moderation Voter Administrator Community Moderator
5 years ago

You can loop over each player and compare their number to the previous highest value like so.

local function FindHighestPlayer()
    local Value,HighestPlayer = 0
    for _,Player in pairs(game.Players:GetPlayers()) do -- loop over all players
        if Player.WhateverValue > Value then -- compare whatever value you want
            Value = Player.WhateverValue -- set
            HigestPlayer = Player -- set
        end
    end
    return HighestPlayer -- return
end

If you have any questions, feel free to leave a comment.

Ad

Answer this question