local W=false local function b(W,K)K(W)for W,v in next,W:GetChildren()do b(v,K)end end local function K(W,b)local K=W repeat K=K.Parent if K==nil then return nil end until K:IsA(b)return K end local function v(W)local K={}b(W,function(W)if W:IsA("\066\097\115\101\080\097\114\116")then K[#K+1]=W end end)return K end local function I(W,b)assert(type(b)=="\116\097\098\108\101","\086\097\108\117\101\115 \105\115 \110\111\116 \097 \116\097\098\108\101")for b,K in next,b do if type(b)=="\110\117\109\098\101\114"then K.Parent=W else W[b]=K end end return W end local function j(W,b)return I(Instance.new(W),b)end local t={"\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 P={"\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 u(W)for b,K in pairs(t)do for b,v in pairs(P)do if W[K].Name==v then return true end end end return false end local function A(b)if W then return false end if u(b)then return false end local K=b:GetConnectedParts()if#K==1 then return false end for W,b in pairs(K)do if u(b)then return false elseif not b:IsDescendantOf(script.Parent)then return false end end return true end local function d(W,b,K,v)K=K or"\087\101\108\100"local t=b: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 P=b:FindFirstChild("\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121")or Instance.new(K)I(P,{Name="\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121",Part0=W;Part1=b;C0=CFrame.new();C1=t and t.Value or b.CFrame:toObjectSpace(W.CFrame),Parent=b})if not t then t=j("\067\070\114\097\109\101\086\097\108\117\101",{Parent=b,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=P.C1})end return P end local function p(W,b,K,v)for W,b in pairs(W)do if A(b)then b:BreakJoints()end end for W,v in pairs(W)do if v~=b then d(b,v,K,b)end end if not v then for W,b in pairs(W)do b.Anchored=false end b.Anchored=false end end local E={}function E.PerfectionWeld(W)local b=K(script,"\084\111\111\108")local I=v(W)local j=b and(b:FindFirstChild("\072\097\110\100\108\101")and(b.Handle:IsA("\066\097\115\101\080\097\114\116")and b.Handle))or W:IsA("\077\111\100\101\108")and W.PrimaryPart or I[1]if j then p(I,j,"\087\101\108\100",false)end return b end return E


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
