Attempt To Index Nil With 'FindFirstChild'

I need help with my script to move lanes like little big planet

I keep getting this error:

  14:11:26.753  Workspace.rusted010101.MoveLane:44: attempt to index nil with 'FindFirstChild'  -  Client - MoveLane:44
  14:11:26.753  Stack Begin  -  Studio
  14:11:26.754  Script 'Workspace.rusted010101.MoveLane', Line 44  -  Studio - MoveLane:44
  14:11:26.754  Stack End  -  Studio
  14:11:27.319  Workspace.rusted010101.MoveLane:32: attempt to index nil with 'FindFirstChild'  -  Client - MoveLane:32
  14:11:27.320  Stack Begin  -  Studio
  14:11:27.320  Script 'Workspace.rusted010101.MoveLane', Line 32  -  Studio - MoveLane:32
  14:11:27.320  Stack End  -  Studio

Heres the script:

local lane = 2
local minlane = 1
local maxlane = 3

local uis = game:GetService("UserInputService")

local plr
local char

game.Players.PlayerAdded:Connect(function(player)
	
	plr = player
	
	plr.CharacterAdded:Connect(function(character)
		
		char = character
		
	end)
	
end)

uis.InputBegan:Connect(function(input, typing)
	
	if not typing then
		
		if input.KeyCode == Enum.KeyCode.W then
			
			if lane ~= maxlane then
			
				lane = lane + 1
				
				char:FindFirstChild("HumanoidRootPart").Position.Z = game.Workspace.Lobby:FindFirstChild("Lane"..lane).Position.Z
				
			end
			
		end
		
		if input.KeyCode == Enum.KeyCode.S then

			if lane ~= minlane then

				lane = lane - 1
				
				char:FindFirstChild("HumanoidRootPart").Position.Z = game.Workspace.Lobby:FindFirstChild("Lane"..lane).Position.Z

			end

		end
		
	end
	
end)
1 Like

is it localscript?
if its local script why dont you use the player variable?

Hey so i kind of fixed it but now im getting an error called “Z cannot be assigned to - Client - MoveLane:21”

Heres the script again:

local lane = 2
local minlane = 1
local maxlane = 3

local uis = game:GetService("UserInputService")

local plr = game.Players.LocalPlayer
local char = game.Players.LocalPlayer.Character


uis.InputBegan:Connect(function(input, typing)
	
	if not typing then
		
		if input.KeyCode == Enum.KeyCode.W then
			
			if lane ~= maxlane then
			
				lane = lane + 1
				
				char:FindFirstChild("HumanoidRootPart").CFrame.Z = game.Workspace.Lobby:FindFirstChild("Lane"..lane).CFrame.Z
				
			end
			
		end
		
		if input.KeyCode == Enum.KeyCode.S then

			if lane ~= minlane then

				lane = lane - 1
				
				char:FindFirstChild("HumanoidRootPart").CFrame.Z = game.Workspace.Lobby:FindFirstChild("Lane"..lane).CFrame.Z

			end

		end
		
	end
	
end)

dont match the position to a position.do the Vector3() or CFrame.new() and then assign your position.
here
image

uhh it still errors
char:FindFirstChild("HumanoidRootPart").Vector3.zAxis=game.Workspace.Lobby:FindFirstChild("Lane"..lane).Vector3.zAxis

Vector3 is not a valid member of Part "Workspace.rusted010101.HumanoidRootPart" - Client - MoveLane:21

char:FindFirstChild("HumanoidRootPart").Position.zAxis=game.Workspace.Lobby:FindFirstChild("Lane"..lane).Position.Z

zAxis cannot be assigned to - Client - MoveLane:21

You cannot replace a single CFrame axis directly as far as I am aware

If you want to replace the position Z then just do

HumanoidRootPart.Position.Z = ...

When that is said, do not use “FindFirstChild” in the same line you’re trying to do something, because this will error if no child is found for whatever you try to reference. :slight_smile:

I have fixed the code by using every single player position and then the single axis

For those who want the code:
char:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(char.HumanoidRootPart.CFrame.X, char.HumanoidRootPart.CFrame.Y, game.Workspace.Lobby:FindFirstChild("Lane"..lane).CFrame.Z)

Make sure not to use FindFirstChild in the same line you’re trying to do something, as mentioned above. Your code should be this instead, to be 100% secure from errors.

local lobby = workspace:WaitForChild("Lobby") -- Place this in the top of the script outside any functions

local rootPart = char.PrimaryPart
local laneObj = lobby:FindFirstChild("Lane"..lane)
if rootPart and laneObj then
	rootPart.CFrame = CFrame.new(rootPart.CFrame.X, rootPart.CFrame.Y, laneObj.CFrame.Z)
end

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