local weld2 = Instance.new("Weld", game.JointsService) weld2.Part0 = Head weld2.Part1 = rEye weld2.C0 = rEye.CFrame:inverse() weld2.C1 = rEye.CFrame:inverse() local weld3 = Instance.new("Weld", game.JointsService) weld3.Part0 = Head weld3.Part1 = lEye weld3.C0 = lEye.CFrame:inverse() weld3.C1 = lEye.CFrame:inverse()
(I have all variables defined and this is the only part of the script that interacts with them)
The C0 = inverse + C1 = inverse operation is meant to get the part back into it's original position after it has been welded (and thus transfered position to part0). This works perfectly if only the first block of code is there, but if the second is there, the rEye and lEye are both wildly offcenter. I don't see how I did this incorrectly, so could someone help?
This is a problem better solved with a generic Model welding algorithm.
My favorite way to go about it is as follows:
When welding two Parts together to preserve their relative CFrames, you will calculate the offset of one of the Parts from the other Part. In our case, the offset of the target child from the "root".
This is best done as follows: offset = rootCFrame:inverse()*childCFrame
. This is because you want to center the child's CFrame around a new coordinate system based upon root's CFrame, hence you invert root's CFrame to get its offset from the origin (which is the current coordinate system). Try drawing this out on a piece of paper to get more of a sense for the logic behind this.
Altogether, a Model welding function may look like the following:
local create = LoadLibrary("RbxUtility").Create -- my favorite built-in ROBLOX object creation function, feel free to use another one function weldModel(model) local children = model:GetChildren() local root = children[1] for index = 2, #children do local child = children[index] if child:IsA("BasePart") then local offsetFromRoot = root.CFrame:inverse()*child.CFrame create("Weld")({ Parent = root, Part0 = root, Part1 = child, C0 = offsetFromRoot }) child.Anchored = false end end root.Anchored = false end weldModel(workspace.Model) -- Replace with a reference to your model