Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use GetChildren() to weld a bunch of parts together at once?

Asked by 7 years ago

The model I am making is a drawer with a bunch of cups inside of it. I inserted a Script into the part of the model that the cups are attaching to so I could weld them together. However, since the cups are all the same and there is a lot of them, I don't want to weld them individually, so I tried to weld them all at once:

drawers = script.Parent

cups = drawers.Parent.Cups:GetChildren()
for _, child in ipairs(cups) do
    CupWeld = Instance.new("Weld")
    CupWeld.Parent = child
    CupWeld.Part0 = child
    CupWeld.Part1 = drawers
    CupWeld.C0 = CFrame.new(child.Position.x,child.Position.y,child.Position.z)-Vector3.new(drawers.Position.X,drawers.Position.Y,drawers.Position.Z)
end

This works, sort of. The cups DO weld to the drawers, but they are all in one place. I want them to be in the position they were in before the script is run.

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
7 years ago
Edited 7 years ago

If you are welding them all to to one part then you need to get the offset of each cup from that part. The CFrame math is weird but all you need to know is that the offset of a cf1 from cf2 is cf1:inverse() * cf2. Also achieved by a simple cf1:toObjectSpace(cf2). This is easy to work in.

You also have to make sure you give the weld a parent at the last step, or else the cups teleport before you set C0.

drawers = script.Parent

cups = drawers.Parent.Cups:GetChildren()
for _, child in ipairs(cups) do
    CupWeld = Instance.new("Weld")
    CupWeld.Part0 = child
    CupWeld.Part1 = drawers
    CupWeld.C0 = child.CFrame:toObjectSpace(drawers.CFrame)
    CupWeld.Parent = child
end

Cheers!

0
No, this still has the same results: all of the cups are welded, but they are all in the same place jamesarsenault 192 — 7y
0
Apologies, it should work now. cabbler 1942 — 7y
Ad

Answer this question