Skip to content

Instantly share code, notes, and snippets.

@FirstVertex
Last active November 15, 2021 23:14
Show Gist options
  • Save FirstVertex/ad6d49b70176850c94ac0589a5d45bc4 to your computer and use it in GitHub Desktop.
Save FirstVertex/ad6d49b70176850c94ac0589a5d45bc4 to your computer and use it in GitHub Desktop.
Roblox Accessories with WeldConstraints have problems. This fixes the shifting that happens when Accessories with welded parts are parented to the Character.
function addAccessoryWithWeldConstraints(humanoid: Humanoid, accessory: Accessory) {
const handle = accessory.FindFirstChild('Handle') as BasePart;
if (!handle) {
return;
}
humanoid.AddAccessory(accessory);
const os = handle.FindFirstChild('OriginalSize') as Vector3Value;
if (os) {
const sizeChange = handle.Size.div(os.Value);
handle.GetDescendants().forEach(desc => {
if (desc.IsA('WeldConstraint')) {
const { Part0, Part1 } = desc;
if (Part0 && Part1) {
const weldCF = Part1.CFrame.ToObjectSpace(Part0.CFrame);
const orientation = weldCF.sub(weldCF.Position);
const weld = new Instance('Weld', desc.Parent);
weld.Part0 = Part0;
weld.Part1 = Part1;
weld.C1 = new CFrame(weldCF.Position.mul(sizeChange)).mul(orientation);
desc.Destroy();
}
}
});
}
}
@FirstVertex
Copy link
Author

Roblox Dev forum post about it is here

Conversion to Lua is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment