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 :)
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 :)
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
Absolutely nothing happens, I tried printing the bestPlayer's name, that did not work.
@cokePanda Oops, sorry I had a typo. I fixed it and tested it, confirmed it's working.
after the variables, why do you put a ";
@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.
2 comments
Use math.max to get the largest number in a table: math.max(1,1,3,5,7,8,9)
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)