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