function Path.Destroy(n)for n,M in ipairs(n._events)do M:Destroy()end n._events=nil n._visualWaypoints=destroyVisualWaypoints(n._visualWaypoints)n._agent=nil n._humanoid=nil n._path:Destroy()n._path=nil n._status=nil n._t=nil n._position=nil n._target=nil end function Path.Stop(n)if not n._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(n)n._status=Path.StatusType.Idle n._visualWaypoints=destroyVisualWaypoints(n._visualWaypoints)n._events.Stopped:Fire(n._model)end function Path.Run(M,n)if not n and(not M._humanoid and M._target)then moveToFinished(M,true)return end if not(n and(typeof(n)=="\086\101\099\116\111\114\051"or n: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()-M._t<=Settings.TIME_VARIANCE and M._humanoid then task.wait(os.clock()-M._t)declareError(M,M.ErrorType.LimitReached)return false elseif M._humanoid then M._t=os.clock()end local h,P=pcall(function()M._path:ComputeAsync(M._agent.PrimaryPart.Position,typeof(n)=="\086\101\099\116\111\114\051"and n or n.Position)end)if not h or M._path.Status==Enum.PathStatus.NoPath or#M._path:GetWaypoints()<2 or M._humanoid and M._humanoid:GetState()==Enum.HumanoidStateType.Freefall then M._visualWaypoints=destroyVisualWaypoints(M._visualWaypoints)task.wait()declareError(M,M.ErrorType.ComputationError)return false end M._status=M._humanoid and Path.StatusType.Active or Path.StatusType.Idle M._target=n pcall(function()M._agent.PrimaryPart:SetNetworkOwner(nil)end)M._waypoints=M._path:GetWaypoints()M._currentWaypoint=2 if M._humanoid then comparePosition(M)end destroyVisualWaypoints(M._visualWaypoints)M._visualWaypoints=M.Visualize and createVisualWaypoints(M._waypoints)M._moveConnection=M._humanoid and(M._moveConnection or M._humanoid.MoveToFinished:Connect(function(...)moveToFinished(M,...)end))if M._humanoid then M._humanoid:MoveTo(M._waypoints[M._currentWaypoint].Position)elseif#M._waypoints==2 then M._target=nil M._visualWaypoints=destroyVisualWaypoints(M._visualWaypoints)M._events.Reached:Fire(M._agent,M._waypoints[2])else M._currentWaypoint=getNonHumanoidWaypoint(M)moveToFinished(M,true)end return true end return Path
--
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
