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 who has the highest of a leaderstat?

I have a round changing script but I need to give the player who has the most points a win at the end of the game.

this is the script:

local seconds = game.Workspace.Time
local ingame = game.Workspace.InGame.Value
local Game = game.Workspace.Game.Position


while true do
 
 wait(1)
 seconds.Value = seconds.Value - 1
  
if seconds.Value == 0 and ingame == 0 then
 
 ingame = 1
 seconds.Value = 20
 
 local teleport = game.Players:GetChildren()
for i = 1, #teleport do
teleport[i].Character:MoveTo(Vector3.new(Game.X, Game.Y, Game.Z))

end
end

if seconds.Value == 0 and ingame == 1 then
 
 ingame = 0
 seconds.Value = 15
 
local teleport = game.Players:GetChildren()
for i = 1, #teleport do
teleport[i]:LoadCharacter()

end
end


end

This is what i tried (I know it doesn't mean anything):

local seconds = game.Workspace.Time
local ingame = game.Workspace.InGame.Value
local Game = game.Workspace.Game.Position


while true do
 
 wait(1)
 seconds.Value = seconds.Value - 1
  
if seconds.Value == 0 and ingame == 0 then
 
 ingame = 1
 seconds.Value = 20
 
 local teleport = game.Players:GetChildren()
for i = 1, #teleport do
teleport[i].Character:MoveTo(Vector3.new(Game.X, Game.Y, Game.Z))

end
end

if seconds.Value == 0 and ingame == 1 then
 
 ingame = 0
 seconds.Value = 15
 
local teleport = game.Players:GetChildren()
for i = 1, #teleport do
teleport[i]:LoadCharacter()

local highestplayer = 
game.Players:GetPlayerFromCharacter(highestplayer)

highestplayer.leaderstats.Wins.Value = highestplayer.leaderstats.Wins.Value + 1

end
end


end

Thanks

2 answers

You can use table.sort.

local player_list = Players:GetPlayers()

table.sort(player_list, function(a, b)
	return a.leaderstats.Wins.Value > b.leaderstats.Wins.Value
end)

This sorts the table based on their wins value, the first one in the table will be the one with the highest value.


I do not like spoon feeding at all, but your code is super messy so I had to rewrite it.

local seconds = game.Workspace.Time
local ingame = game.Workspace.InGame -- Should probably use a BoolValue rather than treating 1 as true and 0 as false. We ain't in C!
local game_position = game.Workspace.Game.Position


while true do
	wait(1)
	seconds.Value = seconds.Value - 1

	if seconds.Value == 0 and ingame.Value == 0 then
		ingame.Value = 1
		seconds.Value = 20

		local player_list = game:GetService("Players"):GetPlayers()
		for _, player in ipairs(player_list) do
			player.Character:MoveTo(game_position)
		end
	end
	if seconds.Value == 0 and ingame.Value == 1 then
		ingame.Value = 0
		seconds.Value = 15
		local player_list = game:GetService("Players"):GetPlayers()
		for _, player in ipairs(player_list) do
			player:LoadCharacter()
			-- Implement the information on getting the highest wins player
		end
	end
end

Sorry if I missed any ends. This is exactly why you indent so you don't miss any/put too many.

To compare which person has the most points, you can use math.max(...) to compare a list of points.

local Table_1={20;50;100};
print( math.max( unpack(Table_1) ) )
--> 100

I unpacked a table because math.max doesn't accept a table but you can unpack a table to replace for tuple arguments.

Then after that, you can use a for loop to find which player(s) has the amount of points.

2 comments

0

No no no what about 0 for loops

0

Table.foreach lol

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