Select children with same name code didn't work

  1. What do you want to achieve? Make the code to select all children with the same name

  2. What is the issue? The code only selected one children and the rest no

  3. What solutions have you tried so far? Tried other codes I found on the forum, did the same issue

Here is the code that im using:

local rope = script.Parent.W.RopeConstraint
local w = script.Parent.W

for i,v in pairs(w:GetChildren()) do
	if v.Name == "RopeConstraint" then
		rope.WinchTarget = 1
	end
end

image

image

As you can see in the image above, only one winch was working

(note: this is my first time posting a post, do inform me if i have any mistakes with it)

2 Likes

You need to reference all parents that are named W.

Code:

--//Variables
local Model = script.Parent

--//Functions
local function InitializePart(part)
	local ropeConstraint = part.RopeConstraint
	ropeConstraint.WinchTarget = 1
end

for i, child in ipairs(Model:GetChildren()) do
	if child.Name == "W" then
		task.spawn(InitializePart, child)
	end
end

From the look of your script, you only selected one W to change WinchTarget. You need to use the pair loop through the model and find if the name is equal to “W”. Then use that same code you provided to change the WinchTarget.

Thanks! It worked.
One question, can it be in the form of variables? So lets say I made a variable (as you see on the code), and there are 4 parts named W that has WinchConstraint in it. On what I know, the variable will only select one part rather than all 4. So, how can I make the variable select all the parts that has the same name rather than only 1?

local rope = script.Parent.W.RopeConstraint
1 Like

There’s 2 ways.

  1. Name each W part differently so you can reference them each
  2. Use a loop like I did

There are no other ways because since all of the parts have the same name, it’s impossible for your script to know which one you’re talking about.

Ah, alright. I don’t yet know much about lua so still making my way on learning it. Thanks for the help!

The first one, did you mean by making each part (with diffrent names) its own variable?
And for the second one, which part of the code you sent is the loop function?

Yes, like naming W to W1, and the other W to W2, then referencing it later.

Loop:

So I’ve tried putting the code you sent earlier into the main script and it worked fine, thanks for the help.
image
One more question, can you explain to me what does the first function in your code does?

local function InitializePart(part)
	local ropeConstraint = part.RopeConstraint
	ropeConstraint.WinchTarget = 1
end
1 Like

It takes in an argument called part. That part is the modified in the function. If you’re confused about task.spawn, I just used it so it won’t yield for each initialization to be finished for the next one to start.

Another way without task.spawn for reference:

--//Variables
local Model = script.Parent

--//Functions
local function InitializePart(part)
	local ropeConstraint = part.RopeConstraint
	ropeConstraint.WinchTarget = 1
end

for i, child in ipairs(Model:GetChildren()) do
	if child.Name == "W" then
		InitializePart(child)
	end
end

I get it now, but not using task.spawn wouldn’t effect much will it

task.spawn will only affect it if you have a lot of stuff going on in the InitializePart or a task.wait(n).

Alright, got it. Thanks for all the help!

2 Likes

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