Here’s a further improved version:
-- qPerfectionWeld.lua
-- Server-side utility that welds every BasePart under the parent
-- to a single root part while skipping any wheel/hinge assemblies.
-- Welds re-apply automatically if the model is re-parented.
--------------------------------------------------------------------
-- Early-out: server only
--------------------------------------------------------------------
local RunService = game:GetService("RunService")
if not RunService:IsServer() then
return
end
--------------------------------------------------------------------
-- Constants / Configuration
--------------------------------------------------------------------
local Container: Instance = script.Parent :: Instance
local WeldName = "AutoWeldConstraint" -- Tag applied to each WeldConstraint
local BreakableJoints = true -- Set false to leave joints intact
-- What counts as a “wheel” joint
local HingeSurfaces: {[string]: boolean} = {
Hinge = true,
Motor = true,
SteppingMotor = true,
}
--------------------------------------------------------------------
-- Helper Functions
--------------------------------------------------------------------
-- 1) Detect wheels without dynamic property access
local function HasWheelJoint(part: BasePart): boolean
return HingeSurfaces[part.TopSurface.Name]
or HingeSurfaces[part.BottomSurface.Name]
or HingeSurfaces[part.LeftSurface.Name]
or HingeSurfaces[part.RightSurface.Name]
or HingeSurfaces[part.FrontSurface.Name]
or HingeSurfaces[part.BackSurface.Name]
end
-- 2) Remove legacy joints manually (BreakJoints is deprecated)
local function DestroyAttachedJoints(part: BasePart)
for _, child in ipairs(part:GetChildren()) do
if child:IsA("JointInstance") then
child:Destroy()
end
end
end
-- 3) Decide whether it is safe to break a part’s joints
local function CanBreak(part: BasePart): boolean
if not BreakableJoints or HasWheelJoint(part) then
return false
end
local connected = part:GetConnectedParts()
if #connected == 1 then -- Only connected to itself
return false
end
for _, other in ipairs(connected) do
if not other:IsDescendantOf(Container) or HasWheelJoint(other) then
return false
end
end
return true
end
-- 4) Weld a pair (skip if already welded)
local function WeldPair(root: BasePart, part: BasePart)
if part:FindFirstChild(WeldName) then
return
end
local weld = Instance.new("WeldConstraint")
weld.Name = WeldName
weld.Part0 = root
weld.Part1 = part
weld.Parent = root
end
-- 5) Collect all BaseParts once
local function CollectParts(scope: Instance): {BasePart}
local list = {}
for _, obj in ipairs(scope:GetDescendants()) do
if obj:IsA("BasePart") then
table.insert(list, obj)
end
end
return list
end
-- 6) Pick a primary part (Tool > Model.PrimaryPart > first part)
local function DetermineRoot(scope: Instance, parts: {BasePart}): BasePart?
local tool = scope:FindFirstAncestorWhichIsA("Tool")
if tool then
local inst = tool:FindFirstChild("Handle", true)
if inst and inst:IsA("BasePart") then
return inst
end
end
if scope:IsA("Model") then
local model = scope :: Model
if model.PrimaryPart then
return model.PrimaryPart
end
end
return parts[1]
end
-- 7) Weld everything to that root
local function WeldAssembly(parts: {BasePart}, root: BasePart, anchorOpt: boolean?)
local anchor: boolean = anchorOpt == true
for _, part in ipairs(parts) do
if CanBreak(part) then
DestroyAttachedJoints(part)
end
if part ~= root then
WeldPair(root, part)
end
part.Anchored = anchor
end
root.Anchored = anchor
end
--------------------------------------------------------------------
-- Main Routine (Re-entrant)
--------------------------------------------------------------------
local function AutoWeld()
local parts = CollectParts(Container)
if #parts == 0 then
warn("qPerfectionWeld: No BaseParts under " .. Container:GetFullName())
return
end
local root = DetermineRoot(Container, parts)
if not root then
warn("qPerfectionWeld: Could not determine a root part for " .. Container:GetFullName())
return
end
WeldAssembly(parts, root, false)
end
--------------------------------------------------------------------
-- Kick Off And Keep Current
--------------------------------------------------------------------
AutoWeld()
Container.AncestryChanged:Connect(AutoWeld)