You're missing the part where you set the C0 (and optionally C1) of the weld.
To explain how this works, I will quote the wiki:
Sometimes, you want to just weld together two parts in their current positions, so that they remain in the same relative positions. This takes just a basic understanding of CFrame math and welds. As previously mentioned,
1 | Part 1. CFrame * C 1 = = Part 0. CFrame * C 0 |
And understanding that
1 | CFrameA:inverse() * CFrameA |
cancels out, we can apply some basic algebra
1 | Part 0. CFrame * C 0 = = Part 1. CFrame * C 1 |
2 | Part 0. CFrame:inverse() * Part 0. CFrame * C 0 = = Part 0. CFrame:inverse() * Part 1. CFrame * C 1 |
3 | C 0 = = Part 0. CFrame:inverse() * Part 1. CFrame * C 1 |
(http://wiki.roblox.com/index.php?title=Weld)
So you need to set the weld's C0 to:
1 | weld.C 0 = PartsToBeWelded [ i ] .CFrame:inverse() * WeldTo.CFrame |
I've run into other problems with welds involving trying to weld Anchored parts, which can usually be solved by setting the weld's parent after you set its other properties, i.e.
01 | WeldTo = script.Parent:WaitForChild( "Base" ) |
02 | PartsToBeWelded = script.Parent:GetChildren() |
03 | for i = 1 , #PartsToBeWelded do |
04 | if PartsToBeWelded [ i ] = = "Part" then |
05 | local weld = Instance.new( "Weld" ) |
06 | weld.C 0 = PartsToBeWelded [ i ] .CFrame:inverse() * WeldTo.CFrame |
07 | weld.Part 0 = PartsToBeWelded [ i ] |
09 | weld.Parent = PartsToBeWelded [ i ] |