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

how do i find out which player has the highest of a leaderstat?

So im trying to make a system where when a map changes, the game finds out which player has the highest of a leaderstat score. i know that math.max() will return the highest number in a set of numbers, but i would need to find out which player has that number, which again, not hard.

My problem being getting the numbers, there doesnt seem to be any easy ways to do this since math.max wont return the highest of a number if you were to do something like for _,numbers in pairs(tableHere) do print(math.max(numbers)) end, so how should i be doing this?

1 answer

You can loop over each player and compare their number to the previous highest value like so.

local function FindHighestPlayer()
	local Value,HighestPlayer = 0
	for _,Player in pairs(game.Players:GetPlayers()) do -- loop over all players
		if Player.WhateverValue > Value then -- compare whatever value you want
			Value = Player.WhateverValue -- set
			HigestPlayer = Player -- set
		end
	end
	return HighestPlayer -- return
end

If you have any questions, feel free to leave a comment.

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