How to make a better caluclating winner formula

The problem is this works if the highest player does not have a same score as someone else. For example player a and player b both have the highest score of 10 points. here us my current formula Basically if the highest person has same score as someone else then the function will return null

local function GetWinner()
local highest = nil
local HighestKillsPlayer = nil

for i,v in pairs(GetContestants()) do
	if v.RoundStats.RoundKills.Value > highest then
		v = HighestKillsPlayer
	end
end

return HighestKillsPlayer

end

You can use table.sort, which sorts the table based on a function (if passed an argument) where it goes through all of the matches in the table and the function will have to return true if value a is greater than value b

table.sort can only sort arrays, since string-indexed keys have no specified order, so if your table is an array of players, you can just sort the table like this:

local tbl = GetContestants()
table.sort(tbl, function(a, b)
    return a.RoundStats.RoundKills.Value > b.RoundStats.RoundKills.Value
end)

local winner = tbl[1]
print(winner)

Otherwise, you’d have to make an array from the table with reference to the players

1 Like

The function GetContestants() is returning an array the problem is my forumla works but I want it so if player A and player B both are the highest then the function should return nil but arent you doing the same thing?

If you sort the table using my function, you can get the highest value and see if other players also have that value:

local tbl = GetContestants()
table.sort(tbl, function(a, b)
    return a.RoundStats.RoundKills.Value > b.RoundStats.RoundKills.Value
end)

local highestValue = tbl[1].RoundStats.RoundKills.Value
local winners = {}

for _, player in pairs(tbl) do
    if player.RoundStats.RoundKills.Value == highestValue then
        table.insert(Winners, player)
    end
end