Round-based system; how to go about winning

So in my game I’m planning to have a round system that will pick a winner based on the greatest amount of a leaderboard value. How would I go about picking the player with the highest number? I have this piece of code so far, and I’m not sure if this is the wrong way to go.

local timer = script.Parent.Parent.Parent.timer.timer.Text
local winner = script.Parent
while true do
	if timer ~= "0" then
		winner.Active = false
	elseif timer == "0" then
		winner.Active = true
		for _, v in pairs(game.Players:GetDescendants()) do
			if v.Name == "leaderstats" then
				
			end
		end
	end
	wait(1)
end
local winner = script.Parent

while true do
    if timer ~= "0" then
        winner.Active = false
    elseif timer == "0" then
        winner.Active = true

        local maxValue = -math.huge 
        local winnerPlayer = nil  

        for _, player in ipairs(game.Players:GetChildren()) do
            local leaderstats = player:FindFirstChild("leaderstats")
            
            if leaderstats and leaderstats.Value > maxValue then
                maxValue = leaderstats.Value
                winnerPlayer = player
            end
        end

    end
    
end
1 Like