Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find the player with the highest score, the second highest score, and so on?

How to find the player with the highest score, the second highest score, and so on? I tried this random thing, but it obviously does not work.

for _, player in pairs(game.Players:GetPlayers()) do
		player.PlayerGui.ScreenGui.Frame.Visible = true
		local first = math.max(player.leaderstats.Score.Value)
		local second = math.min(player.leaderstats.Score.Value)
		player.PlayerGui.ScreenGui.Frame.First.Text = first
		player.PlayerGui.ScreenGui.Frame.Second.Text = second
	end

Thanks!

2 answers

✓ Accepted answer

The easiest way would be to sort the players by score using table.sort.

function higherScoreThan(playerA, playerB)
	return playerA.leaderstats.Score.Value > playerB.leaderstats.Score.Value
end

-- get the list of all the players playing the game that have a Score
local players = {}
for _, player in pairs(game.Players:GetPlayers()) do
	if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Score") then
		table.insert(players, player)
	end
end

table.sort(players, higherScoreThan)

-- display the names
if players[1] then
	FirstPlace.Text = players[1].Name
	if players[2] then
		SecondPlayer.Text = players[2].Name
	else
		SecondPlayer.Text = "only one person was playing :("
	end
else
	FirstPlace.Text = "no one was playing :("
end

1 comment

0

This discussion is preserved as it was. New answers, comments, and votes are closed.