Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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 :)

2 comments

0

Use math.max to get the largest number in a table: math.max(1,1,3,5,7,8,9)

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)

1 answer

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

4 comments

0

Absolutely nothing happens, I tried printing the bestPlayer's name, that did not work.

0

@cokePanda Oops, sorry I had a typo. I fixed it and tested it, confirmed it's working.

0

after the variables, why do you put a ";

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.