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

How do I get the player with the most Kills (leaderstats)?

Asked by 8 years ago

How would I find the player with the highest leaderstats.Kills.Value and then print his name?

I don't want the script to collide when there are two players of the same Kill value, I'd appreciate any help :)

0
Use math.max to get the largest number in a table: math.max(1,1,3,5,7,8,9) Im_Kritz 334 — 8y
0
I currently use this: plrs=game.Players:GetPlayers() table.sort(plrs,function(a,b) return a.leaderstats.Kills.Value > b.leaderstats.Kills.Value end) print(plrs[1].Name), but it outputs with: attempt to index field '?' (a nil value) excellentAnarchy 50 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

This is a simple optimization loop:

function getHighestKills()
    local players = game.Players:GetPlayers();
    local bestSoFar = -1;
    local bestPlayer = nil;

    for i, player in pairs(players) do
        if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Kills") then
            if player.leaderstats.Kills.Value > bestSoFar then
                bestSoFar = player.leaderstats.Kills.Value;
                bestPlayer = player;
            end
        end
    end

    return bestPlayer;
end
0
Absolutely nothing happens, I tried printing the bestPlayer's name, that did not work. excellentAnarchy 50 — 8y
0
@cokePanda Oops, sorry I had a typo. I fixed it and tested it, confirmed it's working. DreadPirateRobux 655 — 8y
0
after the variables, why do you put a "; FiredDusk 1466 — 8y
0
@PreyStar I work as a software developer and primarily develop in Java and C#. Both languages require semicolons at the end of expressions, so it's essentially just habit for me. The Lua interpreter is nice enough to ignore semicolons, so it's just a preference thing. DreadPirateRobux 655 — 8y
Ad

Answer this question