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
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.
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.
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.
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!'
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.