Leader not teleporting to part

Hello!

I’m making a city game, and there’d be a leader who controls it. When the leader dies, another leader gets chosen randomly, and when he dies, so on. I got a script from the Devforum which works, but when I try to add a line where whenever someone becomes leader, the leader gets teleport to a specific spot, it just makes an error:

 12:16:15.677  ServerScriptService.LeaderScript:17: attempt to index nil with 'HumanoidRootPart'  -  Server - LeaderScript:17
  12:16:15.677  Stack Begin  -  Studio
  12:16:15.677  Script 'ServerScriptService.LeaderScript', Line 17  -  Studio - LeaderScript:17
  12:16:15.677  Stack End  -  Studio

I don’t know what’s wrong with it, and it’s been bugging me. Thanks for helping, here’s the script:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false

local function SelectRandomLeader()
	return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end

Players.PlayerAdded:Connect(function(PlayerThatJoined)
	PlayerThatJoined.Team = Teams.Choosing

	if not LockLeaderForFirstJoin then 
		LockLeaderForFirstJoin = true
		CurrentLeader = PlayerThatJoined
		PlayerThatJoined.Team = Teams.Leader
		PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
		print(("%s is the current leader"):format(tostring(CurrentLeader)))
	end

	PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
		CharacterThatJoined.Humanoid.Died:Connect(function()
			CurrentLeader.Team = Teams.Choosing

			local NewLeader = SelectRandomLeader()
			NewLeader.Team = Teams.Leader
			print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader)))
			NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
		end)
	end)
end)

You should execute it when the character gets added (using plr.CharacterAdded event)
So this should probably be the solution:

Players.PlayerAdded:Connect(function(PlayerThatJoined)

	PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
        PlayerThatJoined.Team = Teams.Choosing

	if not LockLeaderForFirstJoin then 
		LockLeaderForFirstJoin = true
		CurrentLeader = PlayerThatJoined
		PlayerThatJoined.Team = Teams.Leader
		PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
		print(("%s is the current leader"):format(tostring(CurrentLeader)))
	end
		CharacterThatJoined.Humanoid.Died:Connect(function()
			CurrentLeader.Team = Teams.Choosing

			local NewLeader = SelectRandomLeader()
			NewLeader.Team = Teams.Leader
			print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader)))
			NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
		end)
	end)
end)

Nope, no errors, the leader didn’t teleport.

Try replacing PlayerThatJoined.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position with CharacterThatJoined.HumanoidRootPart.Position = workspace.CivilianSpawn.Position

Still no errors, leader didn’t teleport, unless I was doing something wrong:

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local CurrentLeader
local LockLeaderForFirstJoin = false

local function SelectRandomLeader()
	return Players:GetPlayers()[Random.new():NextInteger(1, #Players:GetPlayers())]
end

Players.PlayerAdded:Connect(function(PlayerThatJoined)
	PlayerThatJoined.CharacterAdded:Connect(function(CharacterThatJoined)
		PlayerThatJoined.Team = Teams.Choosing

		if not LockLeaderForFirstJoin then 
			LockLeaderForFirstJoin = true
			CurrentLeader = PlayerThatJoined
			PlayerThatJoined.Team = Teams.Leader
			CharacterThatJoined.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
			print(("%s is the current leader"):format(tostring(CurrentLeader)))
		end
		CharacterThatJoined.Humanoid.Died:Connect(function()
			CurrentLeader.Team = Teams.Choosing

			local NewLeader = SelectRandomLeader()
			NewLeader.Team = Teams.Leader
			print(("%s has died. %s is the new leader."):format(tostring(CurrentLeader), tostring(NewLeader)))
			NewLeader.Character.HumanoidRootPart.Position = workspace.CivilianSpawn.Position
		end)
	end)
end)