Local script attempts to index nil with 'FindFirstChild'

Hello.
I was wondering if anyone could help me with this issue I’m having with a script.

Whenever I activate the Function, I get this Error:


I’m confused, since I thought that was the only way to look for spontaneous conditions.
Code(Local):

local UserInputService = game:GetService("UserInputService")
local seat = game.Workspace.Plane2:FindFirstChild("Seat")
local KeyEvent = game.ReplicatedStorage.BombFire
local BombActivate = game.ReplicatedStorage.BombActivate

local function inputBegan(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.R and seat:FindFirstChild("SeatWeld") == true then
		print("The player pressed R while sitting in the seat.")
		KeyEvent:FireServer()
		local Bomb = Instance.new("Part")
		Bomb.Size = Vector3.new(1,1,1)
		Bomb.Shape = "Ball"
		Bomb.BrickColor = BrickColor.new("Deep orange")
		Bomb.Material = Enum.Material.Neon
		Bomb.Position = game.Workspace.Plane2.BombSpawn.Position
		Bomb.Anchored = false
		Bomb.CanCollide = true
		Bomb.Parent = game.Workspace
		BombActivate:FireServer(Bomb)
	end
end

UserInputService.InputBegan:Connect(inputBegan)

Any help is appreciated. =]

The error is basically saying that this:

is returning nil. You could try replacing FindFirstChild with WaitForChild and see if that fixes your issue

No luck. Produces the same error. Should I attach more information?

Might help.


Also, looking at your if statement:
seat:FindFirstChild("SeatWeld") == true

should be just seat:FindFirstChild("SeatWeld") (or equivalently seat:FindFirstChild("SeatWeld") ~= nil).

I don’t believe it is the cause of the error but it would be stopping the script from functioning if the seat existed.

maybe try creating a variable for the seatweld then rewrite line 7 as:
if input.KeyCode == Enum.KeyCode.R and SeatWeld == true then

no, wait i think i see the error

is your instance a boolean? if so, this might be the fix

if input.KeyCode == Enum.KeyCode.R and seat:FindFirstChild(“SeatWeld”) then

Same Error.
I can try making a variable to see when seat weld exists
I’ll write back with results

1 Like

maybe ensuring the code that seat exists before checking the seat world.
if seat and seat:FindFirstChild(“SeatWeld”) then
or the game hasnt loaded in yet and the code hasnt found your seat. on line 2 say:
local seat = game.Worskapce.Plane2:WaitForChild(‘Seat’)

just for testing can you try printing the seat variable and FindFirstChild function before the if statment?

example
local UserInputService = game:GetService("UserInputService")
local seat = game.Workspace.Plane2:FindFirstChild("Seat")
local KeyEvent = game.ReplicatedStorage.BombFire
local BombActivate = game.ReplicatedStorage.BombActivate

local function inputBegan(input, gameProcessedEvent)
	print(seat, seat.FindFirstChild)
	if input.KeyCode == Enum.KeyCode.R and seat:FindFirstChild("SeatWeld") == true then
		print("The player pressed R while sitting in the seat.")
		KeyEvent:FireServer()
		local Bomb = Instance.new("Part")
		Bomb.Size = Vector3.new(1,1,1)
		Bomb.Shape = "Ball"
		Bomb.BrickColor = BrickColor.new("Deep orange")
		Bomb.Material = Enum.Material.Neon
		Bomb.Position = game.Workspace.Plane2.BombSpawn.Position
		Bomb.Anchored = false
		Bomb.CanCollide = true
		Bomb.Parent = game.Workspace
		BombActivate:FireServer(Bomb)
	end
end

UserInputService.InputBegan:Connect(inputBegan)

just to confirm, did you put the wait for child on line 2 or on line 7?

Will report back later, I’ll try all your ideas and reply to each. :slightly_smiling_face:

The error means that your seat variable is nil. The one defined at the start. You can fix this with:

local UserInputService = game:GetService("UserInputService")
local seat = game.Workspace.Plane2:WaitForChild("Seat")
local KeyEvent = game.ReplicatedStorage.BombFire
local BombActivate = game.ReplicatedStorage.BombActivate

local function inputBegan(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.R and seat:FindFirstChild("SeatWeld") == true then
		print("The player pressed R while sitting in the seat.")
		KeyEvent:FireServer()
		local Bomb = Instance.new("Part")
		Bomb.Size = Vector3.new(1,1,1)
		Bomb.Shape = "Ball"
		Bomb.BrickColor = BrickColor.new("Deep orange")
		Bomb.Material = Enum.Material.Neon
		Bomb.Position = game.Workspace.Plane2.BombSpawn.Position
		Bomb.Anchored = false
		Bomb.CanCollide = true
		Bomb.Parent = game.Workspace
		BombActivate:FireServer(Bomb)
	end
end

UserInputService.InputBegan:Connect(inputBegan)

Im not sure but I think this will work. :+1:

I’ve encountered this issue before, and based on the context, it’s most likely related to Streaming properties. I generally don’t recommend changing the streaming settings, as they can introduce other complications. Instead, I prefer finding workarounds.

From what I understand, Plane2 is a model, and inside it, there’s a part called “Seat”. The problem occurs because if the model is too far away, the model itself loads into the workspace, but its child parts do not. This causes them to return nil, and no amount of WaitForChild will fix it.

I’ve used two different methods to solve this:

  1. Using Region3 – I didn’t attempt to find the part at all until the character was within a specific region, as looking for it beforehand was pointless.
  2. Using ChildAdded – I listened for when the missing part was added to the model and referenced it then.

In your case, ChildAdded method should work just fine. I previously implemented this successfully, and while I can’t find the exact script, I tested a new version, and it works fine.

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local planeModelName = "Plane2" -- Name of the plane model
local plane = Workspace:FindFirstChild(planeModelName)
local seat

local KeyEvent = ReplicatedStorage:WaitForChild("BombFire")
local BombActivate = ReplicatedStorage:WaitForChild("BombActivate")

-- Function to update the seat reference when the plane spawns or loads
local function onPlaneAdded(model)
	if model.Name == planeModelName then
		print("Plane model has loaded!")
		seat = model:FindFirstChild("Seat")

		if not seat then
			-- Listen for seat to be added dynamically
			model.ChildAdded:Connect(function(child)
				if child.Name == "Seat" then
					seat = child
					print("Seat has been found!")
				end
			end)
		end
	end
end

-- Check if the plane is already loaded, if not, wait for it to be added
if not plane then
	Workspace.ChildAdded:Connect(onPlaneAdded)
else
	onPlaneAdded(plane)
end

local function inputBegan(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.R and seat and seat:FindFirstChild("SeatWeld") then
		print("The player pressed R while sitting in the seat.")
		KeyEvent:FireServer()

		local Bomb = Instance.new("Part")
		Bomb.Size = Vector3.new(1,1,1)
		Bomb.Shape = "Ball"
		Bomb.BrickColor = BrickColor.new("Deep orange")
		Bomb.Material = Enum.Material.Neon
		Bomb.Position = Workspace.Plane2:FindFirstChild("BombSpawn") and Workspace.Plane2.BombSpawn.Position or Vector3.new(0,0,0) 
		Bomb.Anchored = false
		Bomb.CanCollide = true
		Bomb.Parent = Workspace

		BombActivate:FireServer(Bomb)
	end
end

UserInputService.InputBegan:Connect(inputBegan)

I removed the bomb-related code from my test since it was irrelevant to verifying whether the seat appeared as nil. Instead, I replaced the print statement for "Player pressed R" with my own version.

To ensure the script worked correctly, I placed the model far away, so that the seat inside it wasn’t initially generated. When I walked close enough—or teleported near it—the seat loaded dynamically, and the script adjusted accordingly, detecting and adding the child as expected. Upon sitting in it and pressing R I had no issues.

image
*Edit: My buddy reminded me you can also adjust the Persistent and PersistentPerPlayer on the model as an alternative to make the children appear. I thought I’d post this here in case that is a better solution for you. Good looking out @JohhnyLegoKing

2 Likes

Are you sure “seat” or “plane2” exists in the first place, the error seen here is likely due to that. You should use :WaitForChild() when requiring objects inside workspace (on the client) as the client is affected by Streaming and objects may not be loaded in whenever the client first runs. Here is a corrected version:

local seat = game.Workspace:WaitForChild("Plane2"):WaitForChild("Seat")

Sorry about not replying, I’ll try out all your suggestions and see what works. Thank you all so much.
Will reply soon!

Tried this and added a print at the end of the 2nd FireServer, got nothing.

Works great. I honestly had no idea that Streaming was a thing (Guess I’ve got a lot to learn), but thanks a bunch!
:happy3:

1 Like

I’m glad you found the post and it helped :slight_smile:

1 Like

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