local Q local x local n local Z=0 if canWander then spawn(function()while Z==0 do Z=1 local Q,x=hroot.Position.x+math.random(-WanderX,WanderX),hroot.Position.z+math.random(-WanderZ,WanderZ)human:MoveTo(Vector3.new(Q,0,x))wait(math.random(4,6))Z=0 end end)end while wait()do local N=GetTorso(hroot.Position)if N~=nil then Z=1 local function g(Q)local x=3 if x>#Q then x=3 end if Q[x]==nil and x<#Q then repeat x=x+1 wait()until Q[x]~=nil return Vector3.new(1,0,0)+Q[x]else x=3 return Q[x]end end Q=pfs:FindPathAsync(hroot.Position,N.Position)x=Q:GetWaypoints()n=x local T local y=Vector3.FromNormalId(Enum.NormalId.Front)local V=hroot.CFrame*CFrame.new(y)y=V.p.unit local W=Ray.new(hroot.Position,y)local r,d=game.Workspace:FindPartOnRay(W,hroot)if Q and x or g(x)then if g(x)~=nil and(g(x)).Action==Enum.PathWaypointAction.Walk then human:MoveTo((g(x)).Position)human.Jump=false end if g(x)~=nil and(g(x)).Action==Enum.PathWaypointAction.Jump then human.Jump=true T=human.Changed:connect(function()human.Jump=true end)human:MoveTo((g(x)).Position)else human.Jump=false end if T then T:Disconnect()end else for Q=3,#n,1 do human:MoveTo(n[Q].Position)end end elseif N==nil and canWander then Z=0 Q=nil x=nil human.MoveToFinished:Wait()end end local N=script.Parent:WaitForChild("\072\117\109\097\110\111\105\100")local g=script.Parent:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")local T=script.Parent:WaitForChild("\072\101\097\100")local y=game:GetService("\084\119\101\101\110\083\101\114\118\105\099\101")local V function findTarget()local Q local x=1000 for n,Z in pairs(workspace:GetChildren())do local N=Z:FindFirstChild("\072\117\109\097\110\111\105\100")local T=Z:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if N and(T and Z~=script.Parent)then if(g.Position-T.Position).magnitude<=x and N.Health>0 then x=(g.Position-T.Position).magnitude Q=T end end end return Q end function checkSight(Q)local x=Ray.new(T.Position,(Q.Position-T.Position).Unit*1000)local n,Z=workspace:FindPartOnRayWithIgnoreList(x,{script.Parent})if n then if n:FindFirstChild("\072\117\109\097\110\111\105\100")or n.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")or n.Parent.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")then if math.abs(n.Position.Y-g.Position.Y)<2 then return true else return false end else return false end else return false end end

local path
local waypoint
local oldpoints
local isWandering = 0

if canWander then
	spawn(function()
		while isWandering == 0 do
			isWandering = 1
			local desgx, desgz = hroot.Position.x + math.random(-WanderX, WanderX), hroot.Position.z + math.random(-WanderZ, WanderZ)
			human:MoveTo( Vector3.new(desgx, 0, desgz) )
			wait(math.random(4, 6))
			isWandering = 0
		end
	end)
end

while wait() do
	local enemytorso = GetTorso(hroot.Position)	
	if enemytorso ~= nil then -- if player detected
		isWandering = 1
		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, enemytorso.Position)
		waypoint = path:GetWaypoints()
		oldpoints = waypoint
		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
				human.Jump = true
				connection = human.Changed:connect(function()
					human.Jump = true
				end)
				human:MoveTo( checkw(waypoint).Position )
			else
				human.Jump = false
			end
			
			if connection then
				connection:Disconnect()
			end
			
		else
			for i = 3, #oldpoints do
				human:MoveTo( oldpoints[i].Position )	
			end
		end
	elseif enemytorso == nil and canWander then -- if player not detected
		isWandering = 0
		path = nil
		waypoint = nil
		human.MoveToFinished:Wait()
	end
end
local myHuman = script.Parent:WaitForChild("Humanoid")
local myRoot = script.Parent:WaitForChild("HumanoidRootPart")
local myHead = script.Parent:WaitForChild("Head")
local tweenService = game:GetService("TweenService")
local curTarget

function findTarget()
	local target
	local dist = 1000
	for i,v in pairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local rootPart = v:FindFirstChild("HumanoidRootPart")
		if human and rootPart and v ~= script.Parent then
			if (myRoot.Position - rootPart.Position).magnitude <= dist and human.Health > 0 then
				dist = (myRoot.Position - rootPart.Position).magnitude
				target = rootPart
			end
		end
	end
	
	return target
end

function checkSight(target)
	local ray = Ray.new(myHead.Position,(target.Position - myHead.Position).Unit * 1000)
	local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent})
	
	if hit then
		if hit:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") then
			if math.abs(hit.Position.Y - myRoot.Position.Y) < 2 then
				return true
			else
				return false
			end
		else
			return false
		end
	else
		return false
	end
end
