"Attempt To Call a Nil Value" On GetDescendants() Command With "FindFirstChild()"

Hello

So I’m just starting to get used to the GetDescendants command, and used it a couple times successfully.

But this time, I wasn’t so lucky lol.

I’m trying to get a child from a GetDescendants call

The code:

for _,v in pairs(script.Parent:GetDescendants()) do
	if v:FindFirstChild("ismotor") then
		local system = script.Parent.Parent.Parent.Body.game_System:GetDescendants()
		local motor = Instance.new("Motor")
		motor.Parent = system.FindFirstChild(v.Name)
		motor.Part0 = system.FindFirstChild(v.Name)
		motor.Part1 = v
		motor.MaxVelocity = 0.025
		wait()
		print(motor.Part0.Name)
	end
end```

(Code Context: making hinge motors. I don't want to create each one, so I wanted to make some code to get the descendants and call them by name, and if they match make and set each motor to them)

Whatever advice you can offer would be great!

Thank you

Are you sure you’ve typed game_System correctly? Roblox is erroring because it’s nil, because it doesn’t exist in the Body object.

1 Like

Nope it’s there

Are there multiple objects named Body? What line is the script erroring on?

I think you forgot to add a parent there, unless I’m blind. Try script.Parent.Parent.Parent.Parent.Body

1 Like

Line 7

Nope, didn’t work.

It can reference it (my original code), it just can’t call from it, for some reason

Nevermind, I assumed the highlighted script was the one you were working with here, but clearly not. To confirm this is SetupMotors, right?

1 Like

Yes, it’s the SetupMotors script

Why are you using GetDescendants? It returns a table of Instances (aka objects), but FindFirstChild doesn’t work on tables, it only exists for a single Instance. Remove the :GetDescendants().

Also, replace the . in .FindFirstChild with : so it’s :FindFirstChild, very big difference.

1 Like

Try doing :FindFirstChild rather than .FindFirstChild, didn’t see that until now.

Tables don’t have a function called FindFirstChild associated with them.

1 Like

OHHHH
Sorry that’s what I tried, after the “:” didn’t work.

It does the same thing

Ohhh
welp that sucks

The reason I used “GetDescendant” instead of just “FindFirstChild”
was so I didn’t have to call each value separately, since the main parts are in different models.

Just wanted a one-and-done type of thing

Oh, this is a table? I didn’t see you’re doing this on system which is :GetDescendants.

If you’re trying to find the actual instance of it, you’ll have to do script.Parent.Parent.Parent.Body.game_System:FindFirstChild(v.Name), but quite honestly I’d recommend redoing this script as you’re making it harder than it has to be to do this.

1 Like

. and : for functions do not do the same thing

local SomeTable = {}

function SomeTable.Function1(Text)
    print(Text)
end

function SomeTable:Function2(Text)
    self.Function1(Text) --With :, a self variable is set which points to SomeTable.
end

SomeTable:Function2("Hello, World!") -- Prints 'Hello, World!'
1 Like

Tbh, lowkey.

I just didn’t want to have to define each model. Especially since I planned on reusing this script for different convertible tops (which may have more or less hinges and models)

Pretty much I wanted it to get EVERY part in EACH model above the script.
And then I just get the parts that I want out of what was called.