How do I find the highest value in a group of players

My script is an absolute mess

I want to find the highest value in a group of players , but the system I was using before either indexed the character as nil or just straight up picked the wrong winner.

I’ve scoured all over the internet but anything that I found just wasn’t compatible with my script and I didn’t find a way to make it compatible

I’ve tried using math.max(), and making a table to add the values into, but the issue I ran into was
When I got the highest value, how do I retrace the highest value back to the player
That was the main problem I got throughout the solutions I tried to find.

Could somebody help me?

This is my code

Script
local status = game:GetService("ReplicatedStorage"):WaitForChild("Status")
local serverstorage = game:GetService("ServerStorage")
local inround = game.ReplicatedStorage:WaitForChild("InRound")
local intermissionlength = 10
local increment = 1
local goal = 0
local roundlength = 10
local highest
local teams = game:GetService("Teams")
local playing = teams:WaitForChild("Playing")
local lobby = teams:WaitForChild("Lobby")
local playerservice = game.Players
local obj
local deathmatch = false
local won = false
local eventlength = 2
function resetround()
	roundlength = 10
end
function resetintermission()
	intermissionlength = 10
end
function resetevent()
	eventlength = 2
end
function awardcoins(plr, input)
	print(plr.Name)
	print(plr.ClassName)
	plr:WaitForChild("leaderstats"):WaitForChild("Coins").Value += input
end
function awardlevels(plr, input)
	plr:WaitForChild("Levels"):WaitForChild("Exp").Value += input
end
local clone
local clone1
inround.Changed:Connect(function()
	if inround.Value == true and #playerservice:GetChildren() > 1 then
		for i,v in pairs(playerservice:GetChildren()) do
			local char = v.Character or v:LoadCharacter()
			char.HumanoidRootPart.CFrame = workspace.Goal.CFrame
			v.Team = playing

		end
		while true do
			task.wait(1)
			if #playerservice:GetChildren() <= 1 then
				deathmatch = false
				status.Value = "Not enough players to start the game!"
			end
			if inround.Value == false then
				break
			end
			eventlength -=1
			if deathmatch == true then
				status.Value = "Death match ends in: ".. roundlength .. " seconds"
			else
				status.Value = "Next event in: ".. eventlength .. " seconds"
				if eventlength == goal then
					if clone ~= nil then
						clone:Destroy()
					end
					if clone1 ~= nil then
						clone1:Destroy()
					end
					local random = math.random(1,2)
					if random == 1 then
						local lowgrav = coroutine.create(function()
							for i,v in pairs(game.Players:GetChildren()) do
								if v == nil then
								else
									v.Character.Humanoid.JumpPower = 75
								end
								task.wait(eventlength)
								if v == nil then
								else
									v.Character.Humanoid.JumpPower = 50
								end
							end
						end)
						coroutine.resume(lowgrav)
						status.Value = "Low gravity"
					elseif random == 2 then
						 slippery = coroutine.create(function()
							workspace.Hill.CustomPhysicalProperties.FrictionWeight = 100
							task.wait(eventlength)
							workspace.Hill.CustomPhysicalProperties.FrictionWeight = 50
						end)
						end
					coroutine.resume(slippery)
					status.Value = "The hill is now reaaallllyyy slippery"
					resetevent()
				end
			end
			roundlength -= increment
			if roundlength == 0 then

					if obj ~= nil then

						---v.Character.Humanoid.Health = 0

						status.Value = "And ".. obj.Parent.Name .. " is the winner!"
						print(obj.Name)
						print(obj.Parent.Name)
						awardcoins(game:GetService("Players"):FindFirstChild(obj.Parent.Name), 150)
						deathmatch = false
						inround.Value = false
						resetevent()
						resetround()

				end
			end
			if roundlength == goal then
				if clone ~= nil then
					clone:Destroy()
				end
				if clone1 ~= nil then
					clone1:Destroy()
				end
				inround.Value = false
				resetround()
				break
			end
		end
	elseif inround.Value == false and #playerservice:GetChildren() > 1 then
		for i,v in pairs(playerservice:GetChildren()) do
			local char =  v:LoadCharacter() or v.Character
			char:WaitForChild("HumanoidRootPart").CFrame = workspace.Spectator.CFrame
			v.Team = lobby
		end
		while true do
			if #playerservice:GetChildren() <= 1 then
				status.Value = "Not enough players to start the game!"
				resetintermission()
			end
			task.wait(1)
			status.Value = "Intermission: ".. intermissionlength .. " seconds"
			intermissionlength -= increment
			if intermissionlength == goal then
				inround.Value = true
				intermissionlength = 10
				break
			end
		end
	end
end)
game.Players.PlayerAdded:Connect(function()
	if #playerservice:GetChildren() <= 1 then
		status.Value = "Not enough players to start the game!"
		resetintermission()
		resetround()
	else
		inround.Value = false
	end
end)
game.Players.PlayerRemoving:Connect(function()
	if #playerservice:GetChildren() <= 1 then
		resetintermission()
		resetround()
		status.Value = "Not enough players to start the game!"
	end
end)

The lines I would look into is

if roundlength == 0 then

					if obj ~= nil then

						---v.Character.Humanoid.Health = 0

						status.Value = "And ".. obj.Parent.Name .. " is the winner!"
						print(obj.Name)
						print(obj.Parent.Name)
						awardcoins(game:GetService("Players"):FindFirstChild(obj.Parent.Name), 150)
						deathmatch = false
						inround.Value = false
						resetevent()
						resetround()

				end
			end

this at line 95-111

1 Like

I’m really confused about how to do this

What value of the player should be used in the search? Is there a score object held within the player?

There’s a number value in every player called “Tag”

In that case:

I would recommend looping through all players, and have 2 variables which will get updated as it goes through players.

e.g.

local HighestVal, HighestPlayer

for _, player in game.Players:GetPlayers() do
	if not HighestVal or player.Tag.Value > HighestVal then
		HighestVal = player.Tag.Value
		HighestPlayer = player
	end
end

print(`The winner is {HighestPlayer.Name} with {HighestVal} points!`)

1 Like

Its seems to work! I’m still testing it but I’m pretty sure it works

Thank you so much

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.