What do you want to achieve? Make the code to select all children with the same name
What is the issue? The code only selected one children and the rest no
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
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)
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?
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?
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