local L=script.Parent:WaitForChild("\076\101\102\116\083\108\097\115\104")local a=myHuman:LoadAnimation(L)a.Priority=Enum.AnimationPriority.Action local h=script.Parent:WaitForChild("\082\105\103\104\116\083\108\097\115\104")local u=myHuman:LoadAnimation(h)u.Priority=Enum.AnimationPriority.Action function getUnstuck()myHuman:Move(Vector3.new(math.random(-1,1),0,math.random(-1,1)))wait(.3)end function wander()local L=Vector3.new(myRoot.Position.X+math.random(-120,120),myRoot.Position.Y,myRoot.Position.Z+math.random(-120,120))local a=(game:GetService("\080\097\116\104\102\105\110\100\105\110\103\083\101\114\118\105\099\101")):CreatePath()a:ComputeAsync(myRoot.Position,L)if a.Status==Enum.PathStatus.Success then pathFailCount=0 local L=a:GetWaypoints()for L,a in ipairs(L)do if a.Action==Enum.PathWaypointAction.Jump then myHuman.Jump=true end myHuman:MoveTo(a.Position)if L%5==0 then if findTarget()then break end end local h=myHuman.MoveToFinished:Wait()if not h then break end end else pathFailCount=pathFailCount+1 if pathFailCount>10 then pathFailCount=0 getUnstuck()end end end function findTarget()local L=200 local a=nil for h,u in ipairs(workspace:GetChildren())do local B=u:FindFirstChild("\072\117\109\097\110\111\105\100")local E=u:FindFirstChild("\084\111\114\115\111")or u:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if B and(E and u.Name~=script.Parent.Name)then if(myRoot.Position-E.Position).Magnitude<L and B.Health>0 then a=E L=(myRoot.Position-E.Position).Magnitude end end end return a end function checkSight(L)local a=Ray.new(myRoot.Position,(L.Position-myRoot.Position).Unit*200)local h,u=workspace:FindPartOnRayWithIgnoreList(a,{script.Parent})if h then if h:IsDescendantOf(L.Parent)then return true end end return false end function checkHeight(L)if math.abs(myRoot.Position.Y-L.Position.Y)<5 then return true end return false end function checkDirect(L)if checkSight(L)and(checkHeight(L)and(myRoot.Position-L.Position).Magnitude<30)then return true end return false end function attack(L)if attackCool==true then attackCool=false myHuman:MoveTo(L.Position)myRoot.Swing:Play()local h=math.random(1,2)if h==1 then a:Play()else u:Play()end local B=L.Parent.Humanoid B:TakeDamage(math.random(100,300))myRoot.Hit:Play()if B.Health<1 then wait(.3)end spawn(function()wait(1)attackCool=true end)end end function pathToTarget(L)local a=(game:GetService("\080\097\116\104\102\105\110\100\105\110\103\083\101\114\118\105\099\101")):CreatePath()a:ComputeAsync(myRoot.Position,L.Position)if a.Status==Enum.PathStatus.Success then pathFailCount=0 local h=a:GetWaypoints()for a,u in ipairs(h)do if u.Action==Enum.PathWaypointAction.Jump then myHuman.Jump=true end spawn(function()wait(.5)if myRoot.Position.Y<myHuman.WalkToPoint.Y and chasing==false then myHuman.Jump=true end end)myHuman:MoveTo(u.Position)local B=myHuman.MoveToFinished:Wait()if not B then break end if a%5==0 then if checkDirect(L)then break end if L~=findTarget()then break end end if(h[#h].Position-L.Position).Magnitude>30 then break end end else pathFailCount=pathFailCount+1 if pathFailCount>10 then pathFailCount=0 getUnstuck()end end end function chase(L)chasing=true myHuman:MoveTo(L.Position)end function main()chasing=false local L=findTarget()if L then local a=(myRoot.Position-L.Position).Magnitude if checkDirect(L)then if a>3 then chase(L)end if a<10 then attack(L)end elseif checkSight(L)and(math.abs(myRoot.Position.X-L.Position.X)<1 and math.abs(myRoot.Position.Z-L.Position.Z)<1)then getUnstuck()else pathToTarget(L)end else wander()end end while wait(.1)do if myHuman.Health<1 then break end main()end
--Animation
local leftSlash = script.Parent:WaitForChild("LeftSlash")
local leftSlashAnimation = myHuman:LoadAnimation(leftSlash)
leftSlashAnimation.Priority = Enum.AnimationPriority.Action

local rightSlash = script.Parent:WaitForChild("RightSlash")
local rightSlashAnimation = myHuman:LoadAnimation(rightSlash)
rightSlashAnimation.Priority = Enum.AnimationPriority.Action

function getUnstuck()
	myHuman:Move(Vector3.new(math.random(-1, 1), 0, math.random(-1, 1)))
	wait(0.3)
end

function wander()
	local goal = Vector3.new(
		myRoot.Position.X + math.random(-120, 120),
		myRoot.Position.Y,
		myRoot.Position.Z + math.random(-120, 120)
	)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position, goal)
	if path.Status == Enum.PathStatus.Success then
		pathFailCount = 0
		local waypoints = path:GetWaypoints()
		for i, v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			myHuman:MoveTo(v.Position)
			if i % 5 == 0 then
				if findTarget() then
					break
				end
			end
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then
				break
			end
		end
	else
		pathFailCount = pathFailCount + 1
		if pathFailCount > 10 then
			pathFailCount = 0
			getUnstuck()
		end
	end
end

function findTarget()
	local dist = 200
	local target = nil
	for i, v in ipairs(workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Torso") or v:FindFirstChild("HumanoidRootPart")
		if human and torso and v.Name ~= script.Parent.Name then
			if (myRoot.Position - torso.Position).Magnitude < dist and human.Health > 0 then
				target = torso
				dist = (myRoot.Position - torso.Position).Magnitude
			end
		end
	end
	return target
end

function checkSight(target)
	local ray = Ray.new(myRoot.Position, (target.Position - myRoot.Position).Unit * 200)
	local hit, position = workspace:FindPartOnRayWithIgnoreList(ray, { script.Parent })
	if hit then
		if hit:IsDescendantOf(target.Parent) then
			return true
		end
	end
	return false
end

function checkHeight(target)
	if math.abs(myRoot.Position.Y - target.Position.Y) < 5 then
		return true
	end
	return false
end

function checkDirect(target)
	if checkSight(target) and checkHeight(target) and (myRoot.Position - target.Position).Magnitude < 30 then
		return true
	end
	return false
end

function attack(target)
	if attackCool == true then
		attackCool = false
		myHuman:MoveTo(target.Position)
		myRoot.Swing:Play()
		local attackAnim = math.random(1, 2)
		if attackAnim == 1 then
			leftSlashAnimation:Play()
		else
			rightSlashAnimation:Play()
		end

		local human = target.Parent.Humanoid
		human:TakeDamage(math.random(100, 300))
		myRoot.Hit:Play()
		if human.Health < 1 then
			wait(0.3)
		end

		spawn(function()
			wait(1)
			attackCool = true
		end)
	end
end

function pathToTarget(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(myRoot.Position, target.Position)
	if path.Status == Enum.PathStatus.Success then
		pathFailCount = 0
		local waypoints = path:GetWaypoints()
		for i, v in ipairs(waypoints) do
			if v.Action == Enum.PathWaypointAction.Jump then
				myHuman.Jump = true
			end
			spawn(function()
				wait(0.5)
				if myRoot.Position.Y < myHuman.WalkToPoint.Y and chasing == false then
					myHuman.Jump = true
				end
			end)
			myHuman:MoveTo(v.Position)
			local moveSuccess = myHuman.MoveToFinished:Wait()
			if not moveSuccess then
				break
			end
			if i % 5 == 0 then
				if checkDirect(target) then
					break
				end
				if target ~= findTarget() then
					break
				end
			end
			if (waypoints[#waypoints].Position - target.Position).Magnitude > 30 then
				break
			end
		end
	else
		pathFailCount = pathFailCount + 1
		if pathFailCount > 10 then
			pathFailCount = 0
			getUnstuck()
		end
	end
end

function chase(target)
	chasing = true
	myHuman:MoveTo(target.Position)
end

function main()
	chasing = false
	local target = findTarget()
	if target then
		local targetDistance = (myRoot.Position - target.Position).Magnitude
		if checkDirect(target) then
			if targetDistance > 3 then
				chase(target)
			end
			if targetDistance < 10 then
				attack(target)
			end
		elseif
			checkSight(target)
			and math.abs(myRoot.Position.X - target.Position.X) < 1
			and math.abs(myRoot.Position.Z - target.Position.Z) < 1
		then
			getUnstuck()
		else
			pathToTarget(target)
		end
	else
		wander()
	end
end

while wait(0.1) do
	if myHuman.Health < 1 then
		break
	end
	main()
end
