Attempted to index nil with Wait for Child, (Part) is not a valid member of (Part)

In my Script, I keep getting the error “attempted to index nil with WaitForChild”, or " (xPart) is not a valid member of Model “UFO”. The script still runs as intended but this is really bugging me out, and I have no idea why its throwing errors.

local seat = script.Parent:WaitForChild("Seat")
local LV = script.Parent:WaitForChild("LinearVelocity")


while true do
	if seat.Throttle == 1 then
		if seat.Steer == 0 then
			LV.VectorVelocity = seat.Occupant.Parent.HumanoidRootPart.CFrame.lookVector * 2
		else
			if seat.Steer == 1 then
				-- FR
			else
				-- FL
			end
		end
	elseif seat.Throttle == 0 then
		if seat.Steer == 0 then
			-- idle
		else
			if seat.Steer == 1 then
				-- R
			else
				-- L
			end
		end
	elseif seat.Throttle == -1 then
		if seat.Steer == 0 then
			-- B
		else
			if seat.Steer == 1 then
				-- BR
			else
				-- BL
			end
		end
	end
	task.wait(0.1)
end

1 Like

I have no idea why its doing this, and any help would be greatly appreciated

Probably dont have a child named Seat under the model UFO.

Just a couple of questions:

  1. Is this a localscript or a serverscript?

  2. Have you tried simply doing “script.Parent.LinearVelocity”? (In the case where LinearVelocity exists without creating it through a script).

its there in the workspace
image

its a serverscript, and linearvelocity exists in the workspace as well as seat, neither are created by scripts. The UFO model is stored in ReplicatedStorage, until another script puts it into the workspace, however considering the script is inside the model I don’t see that that could be be causing the error

Unless LinearVelocity is created through a script or you’re requesting it from a localscript, there shouldn’t be any reason to be using :WaitForChild().

You should try using script.Parent.LinearVelocity, and if there’s an error then we can take a look at it and see what we can do.

I Changed it and its still getting the error, thanks for the info though, will do that from now on. But its the Seat that is causing the errors, not the linear velocity.

image
^ this one is from when it teleports you into the seat, it still teleports you into it but comes up with this error for some reason

image
^ and this is when defining seat in the script shown
(I changed seat to script.Parent.Seat as well, and still no change)

I see. Let’s try a different approach and check for all instances inside of the model to get to the root of the problem.

We’ll start with absolutely ensuring that seat is a child of model by using GetChildren():

local YOUR_MODEL = your_model_here;
local modelChildren = YOUR_MODEL:GetChildren();

print(modelChildren);

And we’ll put this respectively at where your code wants to look for the Seat.

image
yup its in there

ok I have a theory, its when the UFO moves from Replicated Storage to workspace that it errors, so perhaps the roblox engine moves the model first and not the script, as its script.Parent that seems to be returning nil
image


It printed the table while it was in replicated storage, then when I transferred it into workspace by clicking E (which is how you “summon” the UFO), it threw this error

I’m unsure about how effective this is, but assuming your script is in ReplicatedStorage and it’s having difficulties with finding the Seat, we can try this approach.

When we clone the UFO into workspace, we will also clone the required script into the model.
We’ll make sure the script is disabled first, and enabled upon being cloned into the UFO model.

oh thats a good Idea, ill let you know how it goes

Ok that fixed the problem with that, the only remaining error is this one
image
After the UFO is parented to the workspace, and has undergone a cutscene of swooping down on the player, the line below teleports the player into the seat, and this is the line which is causing the only remaining error

player.Character.HumanoidRootPart:PivotTo(UFO_.Seat.CFrame) 

I’ve sent you a message since it’s a little awkward to ask here.

All the properties you are indexing were deprecated long time ago (Throttle, Steer)
VehicleSeat - Documentation

player.Character.HumanoidRootPart:PivotTo(UFO_.Seat.CFrame)

I’m not sure on why would this be a problem tho, maybe try :WaitForChild(‘HumanoidRootPart’) instead of using dot notation? not sure…

Did you try using “FindFirstChild” or just doing script.Parent.Seat

tried all 3 of them, wait for child, normal dot, and first child. Me and glass have done some more looking though and it seems to be linked to inputbegan firing twice for some reason

Oh thanks I didn’t even know they had been deprecated, are there any alternatives to it that I could be using, or should steer and throttle be fine?

Use SteerFloat instead of Steer and ThrottleFloat instead of Throttle, or at least this is what the documentation says, has you error fixed itself yet?