function Path.Destroy(L)for L,a in ipairs(L._events)do a:Destroy()end L._events=nil L._visualWaypoints=destroyVisualWaypoints(L._visualWaypoints)L._agent=nil L._humanoid=nil L._path:Destroy()L._path=nil L._status=nil L._t=nil L._position=nil L._target=nil end function Path.Stop(L)if not L._humanoid then output(error,"\065\116\116\101\109\112\116 \116\111 \099\097\108\108 \080\097\116\104\058\083\116\111\112\040\041 \111\110 \097 \110\111\110-\104\117\109\097\110\111\105\100\046")return end disconnectMoveConnection(L)L._status=Path.StatusType.Idle L._visualWaypoints=destroyVisualWaypoints(L._visualWaypoints)L._events.Stopped:Fire(L._model)end function Path.Run(a,L)if not L and(not a._humanoid and a._target)then moveToFinished(a,true)return end if not(L and(typeof(L)=="\086\101\099\116\111\114\051"or L:IsA("\066\097\115\101\080\097\114\116")))then output(error,"\080\097\116\104\102\105\110\100\105\110\103 \116\097\114\103\101\116 \109\117\115\116 \098\101 \097 \118\097\108\105\100 \086\101\099\116\111\114\051 \111\114 \066\097\115\101\080\097\114\116\046")end if os.clock()-a._t<=Settings.TIME_VARIANCE and a._humanoid then task.wait(os.clock()-a._t)declareError(a,a.ErrorType.LimitReached)return false elseif a._humanoid then a._t=os.clock()end local h,u=pcall(function()a._path:ComputeAsync(a._agent.PrimaryPart.Position,typeof(L)=="\086\101\099\116\111\114\051"and L or L.Position)end)if not h or a._path.Status==Enum.PathStatus.NoPath or#a._path:GetWaypoints()<2 or a._humanoid and a._humanoid:GetState()==Enum.HumanoidStateType.Freefall then a._visualWaypoints=destroyVisualWaypoints(a._visualWaypoints)task.wait()declareError(a,a.ErrorType.ComputationError)return false end a._status=a._humanoid and Path.StatusType.Active or Path.StatusType.Idle a._target=L pcall(function()a._agent.PrimaryPart:SetNetworkOwner(nil)end)a._waypoints=a._path:GetWaypoints()a._currentWaypoint=2 if a._humanoid then comparePosition(a)end destroyVisualWaypoints(a._visualWaypoints)a._visualWaypoints=a.Visualize and createVisualWaypoints(a._waypoints)a._moveConnection=a._humanoid and(a._moveConnection or a._humanoid.MoveToFinished:Connect(function(...)moveToFinished(a,...)end))if a._humanoid then a._humanoid:MoveTo(a._waypoints[a._currentWaypoint].Position)elseif#a._waypoints==2 then a._target=nil a._visualWaypoints=destroyVisualWaypoints(a._visualWaypoints)a._events.Reached:Fire(a._agent,a._waypoints[2])else a._currentWaypoint=getNonHumanoidWaypoint(a)moveToFinished(a,true)end return true end return Path
--[[ NON-STATIC METHODS ]]
--
function Path:Destroy()
	for _, event in ipairs(self._events) do
		event:Destroy()
	end
	self._events = nil
	self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
	self._agent = nil
	self._humanoid = nil
	self._path:Destroy()
	self._path = nil
	self._status = nil
	self._t = nil
	self._position = nil
	self._target = nil
end

function Path:Stop()
	if not self._humanoid then
		output(error, "Attempt to call Path:Stop() on a non-humanoid.")
		return
	end
	disconnectMoveConnection(self)
	self._status = Path.StatusType.Idle
	self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
	self._events.Stopped:Fire(self._model)
end

function Path:Run(target)
	--Non-humanoid handle case
	if not target and not self._humanoid and self._target then
		moveToFinished(self, true)
		return
	end

	--Parameter check
	if not (target and (typeof(target) == "Vector3" or target:IsA("BasePart"))) then
		output(error, "Pathfinding target must be a valid Vector3 or BasePart.")
	end

	--Refer to Settings.TIME_VARIANCE
	if os.clock() - self._t <= Settings.TIME_VARIANCE and self._humanoid then
		task.wait(os.clock() - self._t)
		declareError(self, self.ErrorType.LimitReached)
		return false
	elseif self._humanoid then
		self._t = os.clock()
	end

	--Compute path
	local pathComputed, msg = pcall(function()
		self._path:ComputeAsync(
			self._agent.PrimaryPart.Position,
			(typeof(target) == "Vector3" and target) or target.Position
		)
	end)

	--Make sure path computation is successful
	if
		not pathComputed
		or self._path.Status == Enum.PathStatus.NoPath
		or #self._path:GetWaypoints() < 2
		or (self._humanoid and self._humanoid:GetState() == Enum.HumanoidStateType.Freefall)
	then
		self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
		task.wait()
		declareError(self, self.ErrorType.ComputationError)
		return false
	end

	--Set status to active; pathfinding starts
	self._status = (self._humanoid and Path.StatusType.Active) or Path.StatusType.Idle
	self._target = target

	--Set network owner to server to prevent "hops"
	pcall(function()
		self._agent.PrimaryPart:SetNetworkOwner(nil)
	end)

	--Initialize waypoints
	self._waypoints = self._path:GetWaypoints()
	self._currentWaypoint = 2

	--Refer to Settings.COMPARISON_CHECKS
	if self._humanoid then
		comparePosition(self)
	end

	--Visualize waypoints
	destroyVisualWaypoints(self._visualWaypoints)
	self._visualWaypoints = (self.Visualize and createVisualWaypoints(self._waypoints))

	--Create a new move connection if it doesn't exist already
	self._moveConnection = self._humanoid
		and (
			self._moveConnection or self._humanoid.MoveToFinished:Connect(function(...)
				moveToFinished(self, ...)
			end)
		)

	--Begin pathfinding
	if self._humanoid then
		self._humanoid:MoveTo(self._waypoints[self._currentWaypoint].Position)
	elseif #self._waypoints == 2 then
		self._target = nil
		self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
		self._events.Reached:Fire(self._agent, self._waypoints[2])
	else
		self._currentWaypoint = getNonHumanoidWaypoint(self)
		moveToFinished(self, true)
	end
	return true
end

return Path
