Creating a winner choosing system where only the winner gets more points

  1. So I have a bunch of plrs in the players list and once my game ends, i wish to choose a winner out of them. But the winner is based on how many points is in their game.Players.Leaderstats.

  2. I tried looping through the players and getting their points and then use if statements to get the winner. But it still doesn’t get what I wanted.

  3. As this is a little code so most solutions on Yt and the forum are not related to it and hard to find.

I’ll try providing the code later

This is what I came up with:

local Players = game:GetService("Players")

local PlayerWithMostPoints = nil

for _, player in pairs(Players:GetPlayers()) do
	local Points = player:WaitForChild("leaderstats").Points
	if PlayerWithMostPoints == nil then PlayerWithMostPoints = player return end
	if Points.Value > PlayerWithMostPoints:WaitForChild("leaderstats").Points.Value then
		PlayerWithMostPoints = player
	end
end

So you have a variable where you store the player with the most points. You loop through the players, get their points and check if the pojnts value is higher than the player in the variable points variable. If it is, then set the variable to the player.

I also added a check for if the variable is nil or not, because then it will error.
By the end of the for loop, you’ll have the player with the highest points value in the variable.

Sorry for my bad explanation, you’ll get it when you run it though.

Maybe you can make a table for the winner? At the end of the map you can add him to the table and give him the rewards and then you can empty the table.

Thank you so much you just made my code more easily readable. Thx @BandQueenForever

1 Like