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

Can y' help me with welds a bit?

Asked by 9 years ago
01WeldTo = script.Parent:WaitForChild("Base")
02PartsToBeWelded = script.Parent:GetChildren()
03for i = 1, #PartsToBeWelded do
04    if PartsToBeWelded[i] == "Part" then  
05        local weld = Instance.new("Weld")
06        weld.Parent = PartsToBeWelded[i]
07        weld.Part0 = PartsToBeWelded[i]
08        weld.Part1 = WeldTo
09    end
10end

It's a fairly simple script, but it doesn't seem to work. I've no clue as to why, it seems very legitimate. I do in fact have a part called 'Base' in the right spot, and the script is in a model with other parts so..yeah. No clue.

1 answer

Log in to vote
1
Answered by 9 years ago

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,

1Part1.CFrame * C1 == Part0.CFrame * C0

And understanding that

1CFrameA:inverse() * CFrameA

cancels out, we can apply some basic algebra

1Part0.CFrame * C0 == Part1.CFrame * C1
2Part0.CFrame:inverse() * Part0.CFrame * C0 == Part0.CFrame:inverse() * Part1.CFrame * C1
3C0 == Part0.CFrame:inverse() * Part1.CFrame * C1

(http://wiki.roblox.com/index.php?title=Weld)

So you need to set the weld's C0 to:

1weld.C0 = 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.

01WeldTo = script.Parent:WaitForChild("Base")
02PartsToBeWelded = script.Parent:GetChildren()
03for i = 1, #PartsToBeWelded do
04    if PartsToBeWelded[i] == "Part" then  
05        local weld = Instance.new("Weld")
06        weld.C0 = PartsToBeWelded[i].CFrame:inverse() * WeldTo.CFrame
07        weld.Part0 = PartsToBeWelded[i]
08        weld.Part1 = WeldTo
09        weld.Parent = PartsToBeWelded[i]
10    end
11end
Ad

Answer this question