Why am I getting a players character index to nil?

Hi, in the connection 1 in the script below, I’m getting player2.Character = nil for some reason. This is my first time using tables so I’m wondering if its something to do with me doing that wrong?

local function startEvent()
	
	local plrs = {
		
	}
	
	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	table.insert(plrs, player1)
	table.insert(plrs, player2)
	
	local function getPlayer1()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player1 then
				return getplayer
			end
		end
	end
	
	local function getPlayer2()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player2 then
				return getplayer
			end
		end
	end
	
	local player1 = getPlayer1()
	local player2 = getPlayer2()
	
	local char1 = player1.Character
	local char2 = player2.Character
	
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = player1.Character:FindFirstChild("Humanoid")
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation

					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0

					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills


					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
				return true
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				if player1.Character ~= nil and player2.Character ~= nil then
					player2.Character.Humanoid.Health = 100

					if disconnectConnections(plr2CurrentKills) then return end

					wait(1)

					player2.Character.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
					player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
					wait(1)
					local Sword1 = SwordGiver:Clone()
					Sword1.Parent = player1.Backpack
				end
			end)

			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				if disconnectConnections(plr1CurrentKills) then return end

				player1.Character.Humanoid.Health = 100

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack		

			end)
		end
	end		
end
local character = Player2.Character or Player2.CharacterAdded:wait()
2 Likes

Is StandingOnArena1Player2 defined?

1 Like

Yes it is.

Here is the whole script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Player1Spawn
local team2 = workspace.Arena1Player2Spawn

local touchedPart1 = game.Workspace.touchedArena1Plr1
local touchedPart2 = game.Workspace.touchedArena1Plr2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil

local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")

local winner = nil

local function startEvent()
	
	local plrs = {
		
	}
	
	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	table.insert(plrs, player1)
	table.insert(plrs, player2)
	
	local function getPlayer1()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player1 then
				return getplayer
			end
		end
	end
	
	local function getPlayer2()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player2 then
				return getplayer
			end
		end
	end
	
	local player1 = getPlayer1()
	local player2 = getPlayer2()
	
	local char1 = player1.Character
	local char2 = player2.Character
	
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = player1.Character:FindFirstChild("Humanoid")
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation

					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0

					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills


					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
				return true
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				if player1.Character ~= nil and player2.Character ~= nil then
					player2.Character.Humanoid.Health = 100

					if disconnectConnections(plr2CurrentKills) then return end

					wait(1)

					player2.Character.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
					player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
					wait(1)
					local Sword1 = SwordGiver:Clone()
					Sword1.Parent = player1.Backpack
				end
			end)

			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				if disconnectConnections(plr1CurrentKills) then return end

				player1.Character.Humanoid.Health = 100

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack		

			end)
		end
	end		
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		standingOnArena1Player1.RespawnLocation = game.Workspace.Arena1Player1Spawn
		standingOnArena1Player2.RespawnLocation = game.Workspace.Arena1Player2Spawn
		
		part1.Transparency = 1
		part1.CanCollide = false
		part2.Transparency = 1
		part2.CanCollide = false
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

touchedPart1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			part1.Material = Enum.Material.Neon
			standingOnArena1Player1 = Player
		end	
	end
end)

touchedPart1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			part1.Material = Enum.Material.Plastic
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

touchedPart2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			part2.Material = Enum.Material.Neon
			standingOnArena1Player2 = Player
		end	
	end
end)

touchedPart2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			part2.Material = Enum.Material.Plastic
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end





Thanks, where would I put this and why does only player2 have .CharacterAdded:wait() ?

What is the exact error message and which line is it on? We need to know that sort of stuff to be able to provide the best help.

local char1 = player1.Character
Replace this with
local char1 = player1.Character or player1.CharacterAdded:Wait()

You can do this for char2 too ofcourse.

Well before I was getting the error message, attempt to index nil with humanoid. On the line where I have player2.Character.Humanoid.Health. But since I added the if statement before it, I get no error messages, just the script doesn’t work.

It’s still not working for some reason, and no error messages.

Updated script:

local function startEvent()
	
	local plrs = {
		
	}
	
	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2
	
	table.insert(plrs, player1)
	table.insert(plrs, player2)
	
	local function getPlayer1()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player1 then
				return getplayer
			end
		end
	end
	
	local function getPlayer2()
		for idk, getplayer in pairs(plrs) do
			if getplayer == player2 then
				return getplayer
			end
		end
	end
	
	local player1 = getPlayer1()
	local player2 = getPlayer2()
	
	local char1 = player1.Character or player1.CharacterAdded:Wait()
	local char2 = player2.Character or player2.CharacterAdded:Wait()
	
	
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.Visible = true
	game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.Visible = true

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = player1.Character:FindFirstChild("Humanoid")
		local humanoid2 = player2.Character:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = player1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = player2.Backpack
			humanoid2:EquipTool(Sword2)

			print("Approved that i'm humanoid")

			local connections = {}

			local function disconnectConnections(kills)
				if kills == 5 then
					for i, v in ipairs(connections) do
						v:Disconnect()
					end

					if plr1CurrentKills == 5 then
						winner = player1
					end
					if plr2CurrentKills == 5 then
						winner = player2			
					end			

					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation

					if winner == player1 then
						Sword1:Destroy()
						player1.character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
					end

					plr1CurrentKills = 0
					plr2CurrentKills = 0

					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
					game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills


					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
				return true
			end

			connections[1] = player1.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr2CurrentKills = plr2CurrentKills + 1
				
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills
				
				if player1.Character ~= nil and player2.Character ~= nil then
					player2.Character.Humanoid.Health = 100

					if disconnectConnections(plr2CurrentKills) then return end

					wait(1)

					player2.Character.HumanoidRootPart.CFrame = CFrame.new(
						Arena1Player2Spawn.Position
					)
					player2.Character.HumanoidRootPart.CFrame = Arena1Player2Spawn.CFrame
					wait(1)
					local Sword1 = SwordGiver:Clone()
					Sword1.Parent = player1.Backpack
				end
			end)

			connections[2] = player2.CharacterRemoving:Connect(function(hit)
				
				local player1 = getPlayer1()
				local player2 = getPlayer2()

				plr1CurrentKills = plr1CurrentKills + 1

				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface.MainFrame.Main.score_t2.Text = plr2CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t1.Text = plr1CurrentKills
				game.Workspace["Custom Duels Arena"]["The Model"].Monitor.Surface2.MainFrame.Main.score_t2.Text = plr2CurrentKills

				if disconnectConnections(plr1CurrentKills) then return end

				player1.Character.Humanoid.Health = 100

				player1.Character.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				player1.Character.HumanoidRootPart.CFrame = Arena1Player1Spawn.CFrame
				wait(1)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack		

			end)
		end
	end		
end
1 Like

The amount OP’s question is posted; you should have a hotkey for this. /lh

1 Like