My script is getting this error message and I dont know how to fix it

Hello everyone, when I run my script I get this error message on line 110 and i’m not sure how to fix it. I’d really appreciate any help.

’ ServerScriptService.Arena1Script:110: attempt to index nil with ‘Character’’

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 function startEvent()

	local plr1CurrentKills = 0
	local plr2CurrentKills = 0

	local SwordGiver = game.ServerStorage.Sword

	local plr1 = standingOnArena1Player1
	local plr2 = standingOnArena1Player2
	local player1 = standingOnArena1Player1
	local player2 = standingOnArena1Player2

	local Sword1 = SwordGiver:Clone()
	local Sword2 = SwordGiver:Clone()
	local char1 = plr1.Character
	local char2 = plr2.Character
	local Arena1Player1Spawn = game.Workspace.Arena1Player1Spawn
	local Arena1Player2Spawn = game.Workspace.Arena1Player2Spawn

	local character1Loaded = false
	local character2Loaded = false


	print("Variables working fine")


	if char1 or char2 then


		local humanoid1 = char1:FindFirstChild("Humanoid")
		local humanoid2 = char2:FindFirstChild("Humanoid")


		if humanoid1 or humanoid2 then

			Sword1.Parent = plr1.Backpack
			humanoid1:EquipTool(Sword1)
			Sword2.Parent = plr2.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
					
					player1.RespawnLocation = game.Workspace.SpawnLocation
					player2.RespawnLocation = game.Workspace.SpawnLocation


					plr1CurrentKills = 0
					plr2CurrentKills = 0
					part1.Transparency = 0
					part1.CanCollide = true
					part2.Transparency = 0
					part2.CanCollide = true
				end
			end

			connections[1] = plr1.CharacterRemoving:Connect(function(hit)

				local player1 = game.Players:GetPlayerFromCharacter(char1)

				char1 = player1.Character
				local humanoid1 = char1.Humanoid

				plr2CurrentKills = plr2CurrentKills + 1

				disconnectConnections(plr2CurrentKills) -- will check the kills and disconnect connections if it's over 5
				
				local char2 = player2.Character
				
				char2.Humanoid.Health = 100

				char2.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player2Spawn.Position
				)
				workspace[player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
				wait(2)
				local Sword1 = SwordGiver:Clone()
				Sword1.Parent = player1.Backpack
				
			end)
			connections[2] = plr2.CharacterRemoving:Connect(function(hit)

				local player2 = game.Players:GetPlayerFromCharacter(char2)

				char2 = player2.Character
				local humanoid2 = char2.Humanoid
				workspace[player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(0), 0)


				plr1CurrentKills = plr1CurrentKills + 1

				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5
				
				local char1 = player1.Character
				
				char1.Humanoid.Health = 100

				char1.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				workspace[player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
				wait(2)
				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

I believe the error is on this line:

			connections[2] = plr2.CharacterRemoving:Connect(function(hit)

				local player2 = game.Players:GetPlayerFromCharacter(char2)

				char2 = player2.Character -- Here is the error
				local humanoid2 = char2.Humanoid
				workspace[player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(0), 0)


				plr1CurrentKills = plr1CurrentKills + 1

				disconnectConnections(plr1CurrentKills) -- will check the kills and disconnect connections if it's over 5

				local char1 = player1.Character

				char1.Humanoid.Health = 100

				char1.HumanoidRootPart.CFrame = CFrame.new(
					Arena1Player1Spawn.Position
				)
				workspace[player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
				wait(2)
				local Sword2 = SwordGiver:Clone()
				Sword2.Parent = player2.Backpack

			end)

My question is, why are you getting the Character from the Player, and then getting the Character from the Player?

1 Like

No idea hahaha, im new t scripting so still make a lot of mistakes, so you are saying I should delete that line?

I’m pretty sure you should, because this code:

local player2 = game.Players:GetPlayerFromCharacter(char2)
char2 = player2.Character -- Here is the error

Doesn’t achieve anything, however the error, to my knowledge, is this line (correct me if I’m wrong).

I am only confused why there is an error there.

Also sorry for late response.

1 Like