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

Can you help me with player stat comparison?

Asked by 9 years ago

So I have an array that takes the players in the game. I have a generic for loop, with i = 1 being, well, one player. I need to know how to compare it to the rest of the players.

1local players = {}
2for a,b in pairs(game.Players:GetChildren()) do
3    table.insert(players,b)
4end
5 
6for i = 1, #players do
7    if players[i].leaderstats.KOs.Value > players[i].leaderstats.KOs.Value then

I need to find a way to compare player i's Kos to the rest of the players' individually. I am trying to see who gets the most kills. Thanks in advance :)

1 answer

Log in to vote
1
Answered by
Lacryma 548 Moderation Voter
9 years ago

Well to find who has the most kills, it is actually pretty simple. It is similar to what you were doing except, this time we need to keep track of the information through the use of variables.

01function getHighestKills()
02    local kills = 0
03    local player = nil
04 
05    for i,v in pairs(game.Players:GetPlayers()) do
06        local kos = v.leaderstats.KOs.Value
07        if kos > kills then
08            kills = kos
09            player = v
10        end
11    end
12    return player, kills
13end

To use it, simply do this:

1local player, kills = getHighestKills()
2print(player.Name, " ",  kills)
0
Hi :) Thanks for the help! It didn't work though, as when I put it as a function, it said getHighestKills() was a nil global. I tried replacing my for loop with it, removing the return and replacing it with *print:(player)", and it didn't work. Thanks :) UnityAlex 36 — 9y
0
Can you show me how you laid out your script? Lacryma 548 — 9y
Ad

Answer this question