# Getting the most kills from players?

Right now, I am attempting to make a Team Deathmatch and I have gotten my team system and everything sorted out, but I also want to feature players with the most amount of contributed kills to the team, I am hoping that someone could direct me to what system I should use for looking through all the players Kills and finding the top 3 players with the most kills.

2 Likes

What you can do is change the structure of how the amount of kills are stored. Have a table with pairs where the first value is the playerâ€™s name, and second value is how many kills he got.

``````local killCount = {{"player1", 5}, {"player3", 15}, {"player2", 2}} --for example if you had 3 players
``````

Of course this table will be unsorted each time you add a kill to one of the kill counts. When evaluating the top 3 players, you basically sort this array, by comparing the second value of each pair.

``````table.sort(killCount, function(a, b)
return a[2] > b[2]
end)
``````

After sorting it, the 3 first indices should be the top 3 players respectively.

``````local first, second, third = killCount[1][1], killCount[2][1], killCount[3][1] --the [1] each time is the name of the player
``````
2 Likes

Well, first you need to create a table of the team, then you use `table.sort` to sort out the scores from highest to lowest. For example, saying that you store each playerâ€™s kills value in an IntValue stored inside the player:

``````local team = game:GetService("Teams"). --fill in the team here
local plrs = {}

for _, p in pairs(team:GetPlayers()) do
table.insert(plrs, p)
end

table.sort(plrs, function(a, b)
return a.Kills.Value > b.Kills.Value
end)

for i = 1,3 do
if plrs[i] ~= nil then
--will rank the players
end
end
``````
9 Likes