local l=false local function w(l,U)U(l)for l,Z in next,l:GetChildren()do w(Z,U)end end local function U(l,w)local U=l repeat U=U.Parent if U==nil then return nil end until U:IsA(w)return U end local function Z(l)local U={}w(l,function(l)if l:IsA("\066\097\115\101\080\097\114\116")then U[#U+1]=l end end)return U end local function A(l,w)assert(type(w)=="\116\097\098\108\101","\086\097\108\117\101\115 \105\115 \110\111\116 \097 \116\097\098\108\101")for w,U in next,w do if type(w)=="\110\117\109\098\101\114"then U.Parent=l else l[w]=U end end return l end local function N(l,w)return A(Instance.new(l),w)end local o={"\084\111\112\083\117\114\102\097\099\101";"\066\111\116\116\111\109\083\117\114\102\097\099\101";"\076\101\102\116\083\117\114\102\097\099\101","\082\105\103\104\116\083\117\114\102\097\099\101","\070\114\111\110\116\083\117\114\102\097\099\101";"\066\097\099\107\083\117\114\102\097\099\101"}local I={"\072\105\110\103\101";"\077\111\116\111\114","\083\116\101\112\112\105\110\103\077\111\116\111\114"}local function D(l)for w,U in pairs(o)do for w,Z in pairs(I)do if l[U].Name==Z then return true end end end return false end local function H(w)if l then return false end if D(w)then return false end local U=w:GetConnectedParts()if#U==1 then return false end for l,w in pairs(U)do if D(w)then return false elseif not w:IsDescendantOf(script.Parent)then return false end end return true end local function k(l,w,U,Z)U=U or"\087\101\108\100"local o=w:FindFirstChild("\113\082\101\108\097\116\105\118\101\067\070\114\097\109\101\087\101\108\100\086\097\108\117\101")local I=w:FindFirstChild("\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121")or Instance.new(U)A(I,{Name="\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121",Part0=l,Part1=w;C0=CFrame.new();C1=o and o.Value or w.CFrame:toObjectSpace(l.CFrame),Parent=w})if not o then o=N("\067\070\114\097\109\101\086\097\108\117\101",{Parent=w;Name="\113\082\101\108\097\116\105\118\101\067\070\114\097\109\101\087\101\108\100\086\097\108\117\101",Archivable=true,Value=I.C1})end return I end local function r(l,w,U,Z)for l,w in pairs(l)do if H(w)then w:BreakJoints()end end for l,Z in pairs(l)do if Z~=w then k(w,Z,U,w)end end if not Z then for l,w in pairs(l)do w.Anchored=false end w.Anchored=false end end local M={}function M.PerfectionWeld(l)local w=U(script,"\084\111\111\108")local A=Z(l)local N=w and(w:FindFirstChild("\072\097\110\100\108\101")and(w.Handle:IsA("\066\097\115\101\080\097\114\116")and w.Handle))or l:IsA("\077\111\100\101\108")and l.PrimaryPart or A[1]if N then r(A,N,"\087\101\108\100",false)end return w end return M
--Quenty's qPerfectionWeld script remade into a module.

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), --Part1.CFrame:inverse() * Part0.CFrame;-- Part1.CFrame:inverse();
		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 _M = {}

function _M.PerfectionWeld(Model)
	local Tool = GetNearestParent(script, "Tool")
	local Parts = GetBricks(Model)
	local PrimaryPart = Tool and Tool:FindFirstChild("Handle") and Tool.Handle:IsA("BasePart") and Tool.Handle
		or Model:IsA("Model") and Model.PrimaryPart
		or Parts[1]
	if PrimaryPart then
		WeldParts(Parts, PrimaryPart, "Weld", false)
	end
	return Tool
end

return _M
