while wait()do local X=GetTorso(hroot.Position)if X~=nil and human.Health>0 then vars.Wandering.Value=false vars.Chasing.Value=true local function F(X)local F=3 if F>#X then F=3 end if X[F]==nil and F<#X then repeat F=F+1 wait()until X[F]~=nil return Vector3.new(1,0,0)+X[F]else F=3 return X[F]end end path=pfs:FindPathAsync(hroot.Position,X.Position)waypoint=path:GetWaypoints()local E local W=Vector3.FromNormalId(Enum.NormalId.Front)local k=hroot.CFrame*CFrame.new(W)W=k.p.unit local S=Ray.new(hroot.Position,W)local G,d=game.Workspace:FindPartOnRay(S,hroot)if path and waypoint or F(waypoint)then if F(waypoint)~=nil and(F(waypoint)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((F(waypoint)).Position)human.Jump=false end if F(waypoint)~=nil and(F(waypoint)).Action==Enum.PathWaypointAction.Jump then E=human.Changed:connect(function()human.Jump=true end)human:MoveTo(waypoint[4].Position)else human.Jump=false end if E then E:Disconnect()end else for X=3,#waypoint,1 do human:MoveTo(waypoint[X].Position)end end path=nil waypoint=nil elseif X==nil then vars.Wandering.Value=true vars.Chasing.Value=false CchaseName=nil path=nil waypoint=nil human.MoveToFinished:Wait()end end


while wait() do
	local nrstt = GetTorso(hroot.Position)
	if nrstt ~= nil and human.Health > 0 then -- if player detected	
		vars.Wandering.Value = false
		vars.Chasing.Value = true
		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 wait() until t[ci] ~= nil
				return Vector3.new(1,0,0) + t[ci]
			else
				ci = 3
				return t[ci]
			end
		end
		
		path = pfs:FindPathAsync(hroot.Position, nrstt.Position)
		waypoint = path:GetWaypoints()
		local connection;
		
		local direct = Vector3.FromNormalId(Enum.NormalId.Front)
		local ncf = hroot.CFrame * CFrame.new(direct)
		direct = ncf.p.unit
		local rootr = Ray.new(hroot.Position, direct)
		local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot)
		
		if path and waypoint or checkw(waypoint) then
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
				human:MoveTo( checkw(waypoint).Position )
				human.Jump = false
			end
			
			if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
				connection = human.Changed:connect(function()
					human.Jump = true
				end)
				human:MoveTo( waypoint[4].Position )
			else
				human.Jump = false
			end
				
			if connection then
				connection:Disconnect()
			end
			
		else
			for i = 3, #waypoint do
				human:MoveTo( waypoint[i].Position )	
			end
		end
		path = nil
		waypoint = nil
	elseif nrstt == nil then
		vars.Wandering.Value = true
		vars.Chasing.Value = false
		CchaseName = nil
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
	end
