while wait()do local t=GetTorso(hroot.Position)if t~=nil and human.Health>0 then vars.Wandering.Value=false vars.Chasing.Value=true local function H(t)local H=3 if H>#t then H=3 end if t[H]==nil and H<#t then repeat H=H+1 wait()until t[H]~=nil return Vector3.new(1,0,0)+t[H]else H=3 return t[H]end end path=pfs:FindPathAsync(hroot.Position,t.Position)waypoint=path:GetWaypoints()local u local I=Vector3.FromNormalId(Enum.NormalId.Front)local w=hroot.CFrame*CFrame.new(I)I=w.p.unit local i=Ray.new(hroot.Position,I)local G,Z=game.Workspace:FindPartOnRay(i,hroot)if path and waypoint or H(waypoint)then if H(waypoint)~=nil and(H(waypoint)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((H(waypoint)).Position)human.Jump=false end if H(waypoint)~=nil and(H(waypoint)).Action==Enum.PathWaypointAction.Jump then u=human.Changed:connect(function()human.Jump=true end)human:MoveTo(waypoint[4].Position)else human.Jump=false end hroot.Touched:connect(function(H)local I=GetPlayersBodyParts(t)if H:IsA("\080\097\114\116")and(not H.Name==I and(G and(G.Name~=I and(G:IsA("\080\097\114\116")and i:Distance(G.Position)<5))))then u=human.Changed:connect(function()human.Jump=true end)else human.Jump=false end end)if u then u:Disconnect()end else for t=3,#waypoint,1 do human:MoveTo(waypoint[t].Position)end end path=nil waypoint=nil elseif t==nil then vars.Wandering.Value=false 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
			
			hroot.Touched:connect(function(p)
				local bodypartnames = GetPlayersBodyParts(nrstt)
				if p:IsA'Part' and not p.Name == bodypartnames and phit and phit.Name ~= bodypartnames and phit:IsA'Part' and rootr:Distance(phit.Position) < 5 then
					connection = human.Changed:connect(function()
						human.Jump = true
					end)
				else
					human.Jump = false
				end
			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 -- if player not detected
		vars.Wandering.Value = false
		vars.Chasing.Value = false
		CchaseName = nil
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
end
