function Path.Destroy(c)for c,T in ipairs(c._events)do T:Destroy()end c._events=nil if rawget(c,"\095\118\105\115\117\097\108\087\097\121\112\111\105\110\116\115")then c._visualWaypoints=destroyVisualWaypoints(c._visualWaypoints)end c._path:Destroy()setmetatable(c,nil)for T,m in pairs(c)do c[T]=nil end end function Path.Stop(c)if not c._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 if c._status==Path.StatusType.Idle then output(function(c)warn(debug.traceback(c))end,"\065\116\116\101\109\112\116 \116\111 \114\117\110 \080\097\116\104\058\083\116\111\112\040\041 \105\110 \105\100\108\101 \115\116\097\116\101")return end disconnectMoveConnection(c)c._status=Path.StatusType.Idle c._visualWaypoints=destroyVisualWaypoints(c._visualWaypoints)c._events.Stopped:Fire(c._model)end function Path.Run(T,c)if not c and(not T._humanoid and T._target)then moveToFinished(T,true)return end if not(c and(typeof(c)=="\086\101\099\116\111\114\051"or c: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()-T._t<=T._settings.TIME_VARIANCE and T._humanoid then task.wait(os.clock()-T._t)declareError(T,T.ErrorType.LimitReached)return false elseif T._humanoid then T._t=os.clock()end local m,B=pcall(function()T._path:ComputeAsync(T._agent.PrimaryPart.Position,typeof(c)=="\086\101\099\116\111\114\051"and c or c.Position)end)if not m or T._path.Status==Enum.PathStatus.NoPath or#T._path:GetWaypoints()<2 or T._humanoid and T._humanoid:GetState()==Enum.HumanoidStateType.Freefall then T._visualWaypoints=destroyVisualWaypoints(T._visualWaypoints)task.wait()declareError(T,T.ErrorType.ComputationError)return false end T._status=T._humanoid and Path.StatusType.Active or Path.StatusType.Idle T._target=c pcall(function()T._agent.PrimaryPart:SetNetworkOwner(nil)end)T._waypoints=T._path:GetWaypoints()T._currentWaypoint=2 if T._humanoid then comparePosition(T)end destroyVisualWaypoints(T._visualWaypoints)T._visualWaypoints=T.Visualize and createVisualWaypoints(T._waypoints)T._moveConnection=T._humanoid and(T._moveConnection or T._humanoid.MoveToFinished:Connect(function(...)moveToFinished(T,...)end))if T._humanoid then T._humanoid:MoveTo(T._waypoints[T._currentWaypoint].Position)elseif#T._waypoints==2 then T._target=nil T._visualWaypoints=destroyVisualWaypoints(T._visualWaypoints)T._events.Reached:Fire(T._agent,T._waypoints[2])else T._currentWaypoint=getNonHumanoidWaypoint(T)moveToFinished(T,true)end return true end return Path
--
function Path:Destroy()
	for _, event in ipairs(self._events) do
		event:Destroy()
	end
	self._events = nil
	if rawget(self, "_visualWaypoints") then
		self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
	end
	self._path:Destroy()
	setmetatable(self, nil)
	for k, _ in pairs(self) do
		self[k] = nil
	end
end

function Path:Stop()
	if not self._humanoid then
		output(error, "Attempt to call Path:Stop() on a non-humanoid.")
		return
	end
	if self._status == Path.StatusType.Idle then
		output(function(m)
			warn(debug.traceback(m))
		end, "Attempt to run Path:Stop() in idle state")
		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 <= self._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, _ = 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
