--followPath(destination)
-------------------------------------------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------



function AI()

	if Mode == 0 then
		--waypoint = nil
		--print("Patrol mode")
		task.wait(math.random(5,15)/10)
		followPath(hroot.Position + Vector3.new(math.random(-MaxInc, MaxInc), 0, math.random(-MaxInc, MaxInc)))

		--human:MoveTo(zombie.Torso.Position + Vector3.new(math.random(-MaxInc, MaxInc), 0, math.random(-MaxInc, MaxInc)), CurrentPart)

	elseif Mode == 1 then

		target = findNearestTorso(hroot.Position)

		if target ~= nil then
			if CanSee == true and (hroot.Position - target.Position).Magnitude <= MinDistance then
				waypoint = nil
			else

				path:ComputeAsync(hroot.Position, target.Position)
				waypoint = path:GetWaypoints()

				local connection = nil
				local function checkw(t)
					local ci = 3
					if ci > #t then
						ci = 3
					end
					if t[ci] == nil and ci < #t then
						repeat ci = ci + 1 task.wait() until t[ci] ~= nil
						return Vector3.new(1,0,0) + t[ci]
					else
						ci = 3
						return t[ci]
					end
				end
				if path.Status == Enum.PathStatus.Success then
					--print("Chasing "..target.Parent.Name)
					human.WalkToPart = nil
					if  waypoint or checkw(waypoint) then
						if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
							human:MoveTo( checkw(waypoint).Position)
						end


						if connection then
							connection:Disconnect()
						end

					else
						for i = 3, #waypoint do
							human:MoveTo( waypoint[i].Position )	
						end
					end
				else
					--print("No Path,Patrolling")
					task.wait(math.random(5,15)/10)
					followPath(hroot.Position + Vector3.new(math.random(-MaxInc, MaxInc), 0, math.random(-MaxInc, MaxInc)))

				end
			end
		end
	elseif Mode == 2 then

		target = findNearestTorso(hroot.Position)

		if target ~= nil then
			if CanSee == true and (hroot.Position - target.Position).Magnitude <= MinDistance then
				waypoint = {}
				waypoint = nil
			else

				path:ComputeAsync(hroot.Position, target.Position)
				waypoint = path:GetWaypoints()

				local connection = nil
				local function checkw(t)
					local ci = 3
					if ci > #t then
						ci = 3
					end
					if t[ci] == nil and ci < #t then
						repeat ci = ci + 1 task.wait() until t[ci] ~= nil
						return Vector3.new(1,0,0) + t[ci]
					else
						ci = 3
						return t[ci]
					end
				end
				if path.Status == Enum.PathStatus.Success and Memory > 0 then
					--print("Chasing "..target.Parent.Name)
					human.WalkToPart = nil
					if  waypoint or checkw(waypoint) then
						if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
							human:MoveTo( checkw(waypoint).Position )
						end


						if connection then
							connection:Disconnect()
						end

					else
						for i = 3, #waypoint do
							human:MoveTo( waypoint[i].Position )	
						end
					end
				else
					waypoint = nil
					--print("No Path,Patrolling")
					task.wait(math.random(5,15)/10)
					followPath(hroot.Position + Vector3.new(math.random(-MaxInc, MaxInc), 0, math.random(-MaxInc, MaxInc)))

				end
			end
		end
	elseif Mode == 3 then
		--	print("Guarding")
	end	
end