Hello there! I need help...
Anyways. I need to find the player with the highest score. So the script is supposed to find the player with the highest score, and find the score of the player itself. I'm struggling to find the player's name though.
Say, for example we got 3 players. Player1 has score 8; noob has score 1; mlgpro has score 999.
How to make the script know what mlgpro is the player with the highest score and has a score of 999?
Any ideas... anyone? It would be much appreciated! Thanks! ~Pengdoo
A useful pattern you'll need is this loop:
local bestScore local bestPlayer --player with best score local players = game.Players:GetPlayers() local player for i = 1, #players do player = players[i] local score = --[[fill this in here to get the score of player ]] --ex: local score = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Score") and player.leaderstats.Score.Value or 0 if not bestScore or score > bestScore then bestScore = score bestPlayer = player --Note that if score == bestScore then you might have a tie. You could keep track of multiple "bestPlayer"s in a table if you wanted to handle this. end end --At this point, bestPlayer will refer to the player with the most points
bestScore
starts out as accepting any value (in the first iteration), but after that it is only allowed to grow. You only update bestPlayer
when you find a new best score.
Note that though this solution can be made to work with finding the order of people (ex who is in first, second, third, etc), a better solution is to use table.sort
(but this is a separate question).