The old one used a lot of weird and outdated practices so Yeah I updated it to make it more readable and optimized
Old:
local NEVER_BREAK_JOINTS = false
local function CallOnChildren(Instance, FunctionToCall)
FunctionToCall(Instance)
for _, Child in next, Instance:GetChildren() do
CallOnChildren(Child, FunctionToCall)
end
end
local function GetNearestParent(Instance, ClassName)
local Ancestor = Instance
repeat
Ancestor = Ancestor.Parent
if Ancestor == nil then
return nil
end
until Ancestor:IsA(ClassName)
return Ancestor
end
local function GetBricks(StartInstance)
local List = {}
CallOnChildren(StartInstance, function(Item)
if Item:IsA("BasePart") then
List[#List+1] = Item;
end
end)
return List
end
local function Modify(Instance, Values)
assert(type(Values) == "table", "Values is not a table");
for Index, Value in next, Values do
if type(Index) == "number" then
Value.Parent = Instance
else
Instance[Index] = Value
end
end
return Instance
end
local function Make(ClassType, Properties)
return Modify(Instance.new(ClassType), Properties)
end
local Surfaces = {"TopSurface", "BottomSurface", "LeftSurface", "RightSurface", "FrontSurface", "BackSurface"}
local HingSurfaces = {"Hinge", "Motor", "SteppingMotor"}
local function HasWheelJoint(Part)
for _, SurfaceName in pairs(Surfaces) do
for _, HingSurfaceName in pairs(HingSurfaces) do
if Part[SurfaceName].Name == HingSurfaceName then
return true
end
end
end
return false
end
local function ShouldBreakJoints(Part)
if NEVER_BREAK_JOINTS then
return false
end
if HasWheelJoint(Part) then
return false
end
local Connected = Part:GetConnectedParts()
if #Connected == 1 then
return false
end
for _, Item in pairs(Connected) do
if HasWheelJoint(Item) then
return false
elseif not Item:IsDescendantOf(script.Parent) then
return false
end
end
return true
end
local function WeldTogether(Part0, Part1, JointType, WeldParent)
JointType = JointType or "Weld"
local RelativeValue = Part1:FindFirstChild("qRelativeCFrameWeldValue")
local NewWeld = Part1:FindFirstChild("qCFrameWeldThingy") or Instance.new(JointType)
Modify(NewWeld, {
Name = "qCFrameWeldThingy";
Part0 = Part0;
Part1 = Part1;
C0 = CFrame.new();
C1 = RelativeValue and RelativeValue.Value or Part1.CFrame:toObjectSpace(Part0.CFrame);
Parent = Part1;
})
if not RelativeValue then
RelativeValue = Make("CFrameValue", {
Parent = Part1;
Name = "qRelativeCFrameWeldValue";
Archivable = true;
Value = NewWeld.C1;
})
end
return NewWeld
end
local function WeldParts(Parts, MainPart, JointType, DoNotUnanchor)
for _, Part in pairs(Parts) do
if ShouldBreakJoints(Part) then
Part:BreakJoints()
end
end
for _, Part in pairs(Parts) do
if Part ~= MainPart then
WeldTogether(MainPart, Part, JointType, MainPart)
end
end
if not DoNotUnanchor then
for _, Part in pairs(Parts) do
Part.Anchored = false
end
MainPart.Anchored = false
end
end
local function PerfectionWeld()
local Tool = GetNearestParent(script, "Tool")
local Parts = GetBricks(script.Parent)
local PrimaryPart = Tool and Tool:FindFirstChild("Handle") and Tool.Handle:IsA("BasePart") and Tool.Handle or script.Parent:IsA("Model") and script.Parent.PrimaryPart or Parts[1]
if PrimaryPart then
WeldParts(Parts, PrimaryPart, "Weld", false)
else
warn("qWeld - Unable to weld part")
end
return Tool
end
local Tool = PerfectionWeld()
if Tool and script.ClassName == "Script" then
script.Parent.AncestryChanged:connect(function()
PerfectionWeld()
end)
end
New (Thanks @Zhekoay for some of the improvements):
local NEVER_BREAK_JOINTS = false
local function HasWheelJoint(Part)
local HingeSurfaces = {
[Enum.SurfaceType.Hinge] = true,
[Enum.SurfaceType.Motor] = true,
[Enum.SurfaceType.SteppingMotor] = true,
}
return HingeSurfaces[Part.TopSurface]
or HingeSurfaces[Part.BottomSurface]
or HingeSurfaces[Part.LeftSurface]
or HingeSurfaces[Part.RightSurface]
or HingeSurfaces[Part.FrontSurface]
or HingeSurfaces[Part.BackSurface]
end
local function ShouldBreakJoints(Part)
local ConnectedParts = Part:GetConnectedParts()
if NEVER_BREAK_JOINTS or HasWheelJoint(Part) or #ConnectedParts == 1 then
return false
end
for _, Item in ipairs(ConnectedParts) do
if HasWheelJoint(Item) or not Item:IsDescendantOf(script.Parent) then
return false
end
end
return true
end
local function WeldTogether(Part0, Part1, JointType, WeldParent)
JointType = JointType or "Weld"
WeldParent = WeldParent or Part1
if Part0 == Part1 then
warn("Can't weld the same part")
return
end
local RelativeValue = Part1:FindFirstChild("qRelativeCFrameWeldValue")
if not RelativeValue then
RelativeValue = Instance.new("CFrameValue")
RelativeValue.Name = "qRelativeCFrameWeldValue"
RelativeValue.Archivable = true
RelativeValue.Value = Part1.CFrame:ToObjectSpace(Part0.CFrame)
RelativeValue.Parent = Part1
end
local Weld = Part1:FindFirstChild("qCFrameWeldThingy") or Instance.new(JointType)
Weld.Name = "qCFrameWeldThingy"
Weld.Part0 = Part0
Weld.Part1 = Part1
Weld.C0 = CFrame.identity
Weld.C1 = RelativeValue.Value
Weld.Parent = WeldParent
return Weld
end
local function WeldParts(Parts, MainPart, JointType, DoNotUnanchor)
for _, Part in ipairs(Parts) do
if ShouldBreakJoints(Part) then
Part:BreakJoints()
end
WeldTogether(MainPart, Part, JointType, MainPart)
if not DoNotUnanchor then
Part.Anchored = false
end
end
if not DoNotUnanchor then
MainPart.Anchored = false
end
end
local function PerfectionWeld()
local Tool = script:FindFirstAncestorWhichIsA("Tool")
local ToolHandle = Tool and Tool:QueryDescendants("BasePart[#Handle]")[1]
local Parts = script.Parent:QueryDescendants("BasePart")
local PrimaryPart =
ToolHandle or
(script.Parent:IsA("Model") and script.Parent.PrimaryPart) or
(script.Parent:IsA("BasePart") and script.Parent) or
Parts[1]
if PrimaryPart then
WeldParts(Parts, PrimaryPart, "Weld", false)
else
warn("qWeld - Unable to weld part")
end
return Tool
end
if
_VERSION == "Luau" and
typeof(game) == "Instance" and
typeof(script.Parent) == "Instance" and
game:GetService("RunService"):IsServer() and
PerfectionWeld()
then
script.Parent.AncestryChanged:Connect(PerfectionWeld)
end
Feedback would be appreciated!