Hightest Time Player Not working?

Hi, im doing a game like the chosen one, where the player with highest time is owner, but it doesnt work…
Code:

local players = game:GetService("Players")
local highestTime = 0
local highestPlayer
local hdMain = require(game:GetService("ReplicatedStorage"):WaitForChild("HDAdminSetup")):GetMain()
local hd = hdMain:GetModule("API")

for	i,plr in pairs(game.Players:GetPlayers()) do
	if plr.leaderstats.Time.Value > highestTime then
		highestTime = plr.leaderstats.Time.Value
		highestPlayer = plr.Name
		print(highestPlayer)
	end
end

players.PlayerAdded:Connect(function(plr)
	if not highestPlayer then repeat wait() until highestPlayer
		if plr.Name == highestPlayer then
			hd:SetRank(plr, "Server Owner", "Perm")
			plr.Team = game.Teams.Owner
			print("Owner")
		else
			hd:SetRank(plr, "Player", "Perm")
			plr.Team = game.Teams.Players
			print("Player")
		end
	end
	players.PlayerRemoving:Connect(function(plrRemoved)
		if plrRemoved.Name == highestPlayer then
			highestPlayer = nil
		end
		for	i,plr in pairs(game.Players:GetPlayers()) do
			if plr.leaderstats.Time.Value > highestTime then
				highestTime = plr.leaderstats.Time.Value
				highestPlayer = plr.Name
				print(highestPlayer)
			end
		end
		if not highestPlayer then repeat wait() until highestPlayer
			if plr.Name == highestPlayer then
				hd:SetRank(plr, "Server Owner", "Perm")
				plr.Team = game.Teams.Owner
				print("Owner")
			end
		end
	end)
end)

The thing is that it firstly works, and sets the teams, but i have 2 problems,
the hd admin ranks setting doesn’t work, and when the server owner leaves, the second player with higher time doesnt become an owner, an dosent even gets into the team.

(The output doesnt show any errors, but neither prints anything.)

its because of the infinite loop that checks if they are the highest player, it never updates the original highest player/time because it can’t ever get to the function that checks whenever a player is removed.

You should move the “players.PlayerRemoving:Connect(function(plrRemoved)” to outside of the player connect

(I don’t know how to fix the hd admin rank because i’ve never used hd admin in my life)

1 Like

Thx!


1 Like

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