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
WeldTo = script.Parent:WaitForChild("Base")
PartsToBeWelded = script.Parent:GetChildren()
for i = 1, #PartsToBeWelded do
    if PartsToBeWelded[i] == "Part" then   
        local weld = Instance.new("Weld")
        weld.Parent = PartsToBeWelded[i]
        weld.Part0 = PartsToBeWelded[i]
        weld.Part1 = WeldTo
    end
end

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 8 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,

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

And understanding that

CFrameA:inverse() * CFrameA

cancels out, we can apply some basic algebra

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

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

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

weld.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.

WeldTo = script.Parent:WaitForChild("Base")
PartsToBeWelded = script.Parent:GetChildren()
for i = 1, #PartsToBeWelded do
    if PartsToBeWelded[i] == "Part" then   
        local weld = Instance.new("Weld")
        weld.C0 = PartsToBeWelded[i].CFrame:inverse() * WeldTo.CFrame
        weld.Part0 = PartsToBeWelded[i]
        weld.Part1 = WeldTo
        weld.Parent = PartsToBeWelded[i]
    end
end
Ad

Answer this question