In my game, you get points every time you survive a round. At the end of 3 rounds, I want it to say that the player with the most points is the winner and award them a win. How exactly would I go about doing something like this? I've thought about using tables or arrays but I'm not quite sure how exactly.
COMMUNITY QUESTION
How would I go about picking a player from the leaderboard with the most points?
1 answer
You would loop through all of the players and check their appropriate value. If the value is bigger than the current largest, the current largest gets reassigned to the new largest.
For example;
local function GetWinner()
local Largest
for _,Player in pairs (game.Players:GetPlayers()) do -->> Loop through all of the players
local Stat = Player:FindFirstChild("leaderstats"):FindFirstChild("Points") -->> Get the stat
-->> If the largest hasn't been assigned yet or the player's stat is bigger
if not Largest or Largest.Stat < Stat.Value then
Largest = {Player = Player,Stat = Stat.Value} -->> Set the largest to the player
end
end
return Largest.Player
end
print(GetWinner().Name.. " is the winner!")
Hope I helped!
~TDP
This discussion is preserved as it was. New answers, comments, and votes are closed.
