local function G(G)local N=G._waypoints[G._currentWaypoint-1]local t=G._waypoints[G._currentWaypoint]G._events.WaypointReached:Fire(G._agent,N,t)end local function N(N,t)if not N._humanoid then if t and N._currentWaypoint+1<=#N._waypoints then G(N)N._currentWaypoint+=1 elseif t then N._visualWaypoints=destroyVisualWaypoints(N._visualWaypoints)N._target=nil N._events.Reached:Fire(N._agent,N._waypoints[N._currentWaypoint])else N._visualWaypoints=destroyVisualWaypoints(N._visualWaypoints)N._target=nil declareError(N,N.ErrorType.TargetUnreachable)end return end if t and N._currentWaypoint+1<=#N._waypoints then G(N)N._currentWaypoint+=1 move(N)elseif t then disconnectMoveConnection(N)N._status=Path.StatusType.Idle N._visualWaypoints=destroyVisualWaypoints(N._visualWaypoints)N._events.Reached:Fire(N._agent,N._waypoints[N._currentWaypoint])else disconnectMoveConnection(N)N._status=Path.StatusType.Idle N._visualWaypoints=destroyVisualWaypoints(N._visualWaypoints)declareError(N,N.ErrorType.TargetUnreachable)end end
--Fire the WaypointReached event
local function invokeWaypointReached(self)
	local lastWaypoint = self._waypoints[self._currentWaypoint - 1]
	local nextWaypoint = self._waypoints[self._currentWaypoint]
	self._events.WaypointReached:Fire(self._agent, lastWaypoint, nextWaypoint)
end

local function moveToFinished(self, reached)
	--Handle case for non-humanoids
	if not self._humanoid then
		if reached and self._currentWaypoint + 1 <= #self._waypoints then
			invokeWaypointReached(self)
			self._currentWaypoint += 1
		elseif reached then
			self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
			self._target = nil
			self._events.Reached:Fire(self._agent, self._waypoints[self._currentWaypoint])
		else
			self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
			self._target = nil
			declareError(self, self.ErrorType.TargetUnreachable)
		end
		return
	end

	if reached and self._currentWaypoint + 1 <= #self._waypoints then --Waypoint reached
		invokeWaypointReached(self)
		self._currentWaypoint += 1
		move(self)
	elseif reached then --Target reached, pathfinding ends
		disconnectMoveConnection(self)
		self._status = Path.StatusType.Idle
		self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
		self._events.Reached:Fire(self._agent, self._waypoints[self._currentWaypoint])
	else --Target unreachable
		disconnectMoveConnection(self)
		self._status = Path.StatusType.Idle
		self._visualWaypoints = destroyVisualWaypoints(self._visualWaypoints)
		declareError(self, self.ErrorType.TargetUnreachable)
	end
end
