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.)