I’m trying to make a system where the person with the highest kill streak has a BillBoardGui on their head that can be seen all throughout the map. Though I’m stumped on how I can loop through every player’s leaderstats, etc. Any help or guidance will be great.
For this, you can use math.max and unpack. math.max returns the highest value out of the given numbers, and unpack will return a given table’s elements as individual values.
local numbers = {1, 2, 3, 4}
print(unpack(numbers)) --> 1 2 3 4
print(math.max(unpack(numbers))) --> 4
Using this, you can loop through the players and insert each killstreak leaderstat into a table and get the highest number from there.
How could I keep track of which player has that kill streak in the table though? math.max only accepts numbers.
I changed my approach on this, so here is what I came up with. What I told you before would also work but it takes more work as you can’t compare the killstreaks as easily. This method is pretty much the same as Moonvane’s, but I shortened it a bit.
local function getLeader()
local leadingKills, leadingPlayer = 0, nil
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if player.Kills.Value > leadingKills then
leadingKills, leadingPlayer = player.leaderstats.Kills.Value, player
end
end
return leadingPlayer
end
local leadingPlayer = getLeader()
That does work, but in order to find out which player has it you need to use a standard smallest check function, similar to the function the classic NPCs use to follow the closest player:
function findChosenPlayer()
local lastHighest = -math.huge
local chosenPlayer;
for _, v in pairs(game:GetService("Players"):GetPlayers()) do
local this = v.leaderstats.Kills.Value
if this > lastHighest then
lastHighest = this
chosenPlayer = v
end
end
return chosenPlayer
end
local playerWithHighestKills = findChosenPlayer()