Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

2 comments

0

No, this still has the same results: all of the cups are welded, but they are all in the same place

0

Apologies, it should work now.

This discussion is preserved as it was. New answers, comments, and votes are closed.