How to fix "Attempt to index nil with Character"

script.Parent.Parent.TouchDec.Touched:Connect(function(Touch1)
	if script.Parent.SurfaceGui.ScreenFrame.ScreenText.Text == "1/2" then
		local Player1 = game:GetService("Players"):GetPlayerFromCharacter(Touch1.Parent)
		local C1 = Player1.Character
		script.Parent.Parent.TouchDec.Touched:Connect(function(Touch2)
			if script.Parent.SurfaceGui.ScreenFrame.ScreenText.Text == "2/2" then
				local Player2 = game:GetService("Players"):GetPlayerFromCharacter(Touch2.Parent)
				local C2 = Player2.Character
				Player1.DevTouchMovementMode = Enum.DevTouchMovementMode.Scriptable
				Player2.DevTouchMovementMode = Enum.DevTouchMovementMode.Scriptable
				Player1.DevComputerMovementMode = Enum.DevComputerMovementMode.Scriptable
				Player2.DevComputerMovementMode = Enum.DevComputerMovementMode.Scriptable
				wait(0.1)
				C1:SetPrimaryPartCFrame(script.Parent.Parent.Player1Area.CFrame + Vector3.new(0,1,0))
				C2:SetPrimaryPartCFrame(script.Parent.Parent.Player2Area.CFrame + Vector3.new(0,1,0))
				script.Parent.Parent.Border.CanCollide = true
			end
		end)
	end
end)

I have this script and I am getting the output error “Attempt to index nil with Character” on line 8

local C2 = Player2.Character

although I am doing the same with C1 and it is working fine

Couldn’t C2 just be Touch2.Parent?

Yea but I don’t want to do that cause the Touch2 may be the right foot or the left foot etc so it’s not a fixed thing

use local C2 = Player2.LocalPlayer.Character
I suppose you are accessing the character from the players

I’d recommend using :FindFirstAncestorOfClass(“Model”)

local Players = game:GetService("Players");

script.Parent.Parent.TouchDec.Touched:Connect(function(Touch1)
  local Model1 = Touch1:FindFirstAncestorOfClass("Model");
  local Player1 = if Model1 then Players:GetPlayerFromCharacter(Model1) else nil;
  if Player1 then -- you want to double check and make sure it's a valid player before moving forward
    -- C1 can just be replaced with Model1 no reason to make a new variable
  end
end)

the same logic should be applied for C2, and I wouldn’t put them both in the same logical process

Player2 wouldn’t have a LocalPlayer, the only time LocalPlayer exists is from a client view and it’s an attribute/property of the Players service itself not a Player object.

But the question is why the C1 works and the C2 not although they are the same

It’s entirely dependent on what the .Touch is hitting, if it first hits an accessory part then .Parent wouldn’t be the character model i.e it wouldn’t be able to pull a player from it → which is why I recommended FindFirstAncestorOfClass it ensures that it grabs the closest top instance of class model (the only time this may cause issues is if you have models of parts inside the player model)

I found a solution I just made and if statement to check if the touch is a HRP so I make sure that touch.parent is the character also siren solution still works so I will mark his reply as a solution

1 Like

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