local function N()return maid.humanoid.Health>0 and maid.humanoid:GetState()~=Enum.HumanoidStateType.Dead end local function p()maid:destroy()end local function f()while N()do if not attacking then local N=getRandomPointInCircle(startPosition,PATROL_RADIUS)maid.humanoid.WalkSpeed=PATROL_WALKSPEED maid.humanoid:MoveTo(N)end wait(random:NextInteger(MIN_REPOSITION_TIME,MAX_REPOSITION_TIME))end end local function z(N)local p=N and(N.Parent and N.Parent:FindFirstChild("\072\117\109\097\110\111\105\100"))if not p then return false end local f=false local z=(maid.humanoidRootPart.Position-N.Position).Magnitude if z<=ATTACK_RADIUS then local D=Ray.new(maid.humanoidRootPart.Position,(N.Parent.HumanoidRootPart.Position-maid.humanoidRootPart.Position).Unit*z)local y=Workspace:FindPartOnRayWithIgnoreList(D,{N.Parent;maid.instance},false,true)if N~=maid.instance and(N:IsDescendantOf(Workspace)and(p.Health>0 and(p:GetState()~=Enum.HumanoidStateType.Dead and(not CollectionService:HasTag(N.Parent,"\090\111\109\098\105\101\070\114\105\101\110\100")and not y))))then f=true end end return f end local function D()if not searchingForTargets and tick()-timeSearchEnded>=SEARCH_DELAY then searchingForTargets=true local N=maid.humanoidRootPart.Position local p=N+Vector3.new(ATTACK_RADIUS,ATTACK_RADIUS,ATTACK_RADIUS)local f=N+Vector3.new(-ATTACK_RADIUS,-ATTACK_RADIUS,-ATTACK_RADIUS)searchRegion=Region3.new(f,p)searchParts=Workspace:FindPartsInRegion3(searchRegion,maid.instance,math.huge)newTarget=nil newTargetDistance=nil searchIndex=1 end if searchingForTargets then local N=0 while searchingForTargets and(searchIndex<=#searchParts and N<MAX_PARTS_PER_HEARTBEAT)do local p=searchParts[searchIndex]if p and z(p)then local N=p.Parent local f=(N.HumanoidRootPart.Position-maid.humanoidRootPart.Position).magnitude if not newTargetDistance or f<newTargetDistance then newTarget=N.HumanoidRootPart newTargetDistance=f end end searchIndex=searchIndex+1 N=N+1 end if searchIndex>=#searchParts then target=newTarget searchingForTargets=false timeSearchEnded=tick()end end end local function y()local N=(maid.humanoidRootPart.Position-target.Position).Unit*ATTACK_RANGE+target.Position maid.humanoid:MoveTo(N)if not movingToAttack then maid.humanoid.WalkSpeed=random:NextInteger(ATTACK_MIN_WALKSPEED,ATTACK_MAX_WALKSPEED)end movingToAttack=true maid.attackAnimation:Stop()end local function H()attacking=true lastAttackTime=tick()local N=maid.humanoid.WalkSpeed maid.humanoid.WalkSpeed=0 maid.attackAnimation:Play()local p=Instance.new("\080\097\114\116")p.Size=HITBOX_SIZE p.Transparency=1 p.CanCollide=true p.Anchored=true p.CFrame=maid.humanoidRootPart.CFrame*CFrame.new(0,-1,-3)p.Parent=Workspace local f=p:GetTouchingParts()p:Destroy()local D={}for N,p in pairs(f)do local f=p:FindFirstAncestorOfClass("\077\111\100\101\108")if z(p)and not D[f]then D[f.Humanoid]=true end end for N in pairs(D)do N:TakeDamage(ATTACK_DAMAGE)end startPosition=maid.instance.PrimaryPart.Position wait(ATTACK_STAND_TIME)maid.humanoid.WalkSpeed=N maid.attackAnimation:Stop()attacking=false end
--[[
	Implementation
]]

local function isAlive()
	return maid.humanoid.Health > 0 and maid.humanoid:GetState() ~= Enum.HumanoidStateType.Dead
end

local function destroy()
	maid:destroy()
end

local function patrol()
	while isAlive() do
		if not attacking then
			local position = getRandomPointInCircle(startPosition, PATROL_RADIUS)
			maid.humanoid.WalkSpeed = PATROL_WALKSPEED
			maid.humanoid:MoveTo(position)
		end

		wait(random:NextInteger(MIN_REPOSITION_TIME, MAX_REPOSITION_TIME))
	end
end

local function isInstaceAttackable(targetInstance)
	local targetHumanoid = targetInstance and targetInstance.Parent and targetInstance.Parent:FindFirstChild("Humanoid")
	if not targetHumanoid then
		return false
	end

	local isAttackable = false
	local distance = (maid.humanoidRootPart.Position - targetInstance.Position).Magnitude

	if distance <= ATTACK_RADIUS then
		local ray = Ray.new(
			maid.humanoidRootPart.Position,
			(targetInstance.Parent.HumanoidRootPart.Position - maid.humanoidRootPart.Position).Unit * distance
		)

		local part = Workspace:FindPartOnRayWithIgnoreList(ray, {
			targetInstance.Parent,
			maid.instance,
		}, false, true)

		if
			targetInstance ~= maid.instance
			and targetInstance:IsDescendantOf(Workspace)
			and targetHumanoid.Health > 0
			and targetHumanoid:GetState() ~= Enum.HumanoidStateType.Dead
			and not CollectionService:HasTag(targetInstance.Parent, "ZombieFriend")
			and not part
		then
			isAttackable = true
		end
	end

	return isAttackable
end

local function findTargets()
	-- Do a new search region if we are not already searching through an existing search region
	if not searchingForTargets and tick() - timeSearchEnded >= SEARCH_DELAY then
		searchingForTargets = true

		-- Create a new region
		local centerPosition = maid.humanoidRootPart.Position
		local topCornerPosition = centerPosition + Vector3.new(ATTACK_RADIUS, ATTACK_RADIUS, ATTACK_RADIUS)
		local bottomCornerPosition = centerPosition + Vector3.new(-ATTACK_RADIUS, -ATTACK_RADIUS, -ATTACK_RADIUS)

		searchRegion = Region3.new(bottomCornerPosition, topCornerPosition)
		searchParts = Workspace:FindPartsInRegion3(searchRegion, maid.instance, math.huge)

		newTarget = nil
		newTargetDistance = nil

		-- Reset to defaults
		searchIndex = 1
	end

	if searchingForTargets then
		-- Search through our list of parts and find attackable humanoids
		local checkedParts = 0
		while searchingForTargets and searchIndex <= #searchParts and checkedParts < MAX_PARTS_PER_HEARTBEAT do
			local currentPart = searchParts[searchIndex]
			if currentPart and isInstaceAttackable(currentPart) then
				local character = currentPart.Parent
				local distance = (character.HumanoidRootPart.Position - maid.humanoidRootPart.Position).magnitude

				-- Determine if the charater is the closest
				if not newTargetDistance or distance < newTargetDistance then
					newTarget = character.HumanoidRootPart
					newTargetDistance = distance
				end
			end

			searchIndex = searchIndex + 1

			checkedParts = checkedParts + 1
		end

		if searchIndex >= #searchParts then
			target = newTarget
			searchingForTargets = false
			timeSearchEnded = tick()
		end
	end
end

local function runToTarget()
	local targetPosition = (maid.humanoidRootPart.Position - target.Position).Unit * ATTACK_RANGE + target.Position

	maid.humanoid:MoveTo(targetPosition)

	if not movingToAttack then
		maid.humanoid.WalkSpeed = random:NextInteger(ATTACK_MIN_WALKSPEED, ATTACK_MAX_WALKSPEED)
	end

	movingToAttack = true

	-- Stop the attack animation
	maid.attackAnimation:Stop()
end

local function attack()
	attacking = true
	lastAttackTime = tick()

	local originalWalkSpeed = maid.humanoid.WalkSpeed
	maid.humanoid.WalkSpeed = 0

	-- Play the attack animation
	maid.attackAnimation:Play()

	-- Create a part and use it as a collider, to find humanoids in front of the zombie
	-- This is not ideal, but it is the simplest way to achieve a hitbox
	local hitPart = Instance.new("Part")
	hitPart.Size = HITBOX_SIZE
	hitPart.Transparency = 1
	hitPart.CanCollide = true
	hitPart.Anchored = true
	hitPart.CFrame = maid.humanoidRootPart.CFrame * CFrame.new(0, -1, -3)
	hitPart.Parent = Workspace

	local hitTouchingParts = hitPart:GetTouchingParts()

	-- Destroy the hitPart before it results in physics updates on touched parts
	hitPart:Destroy()

	-- Find humanoids to damage
	local attackedHumanoids = {}
	for _, part in pairs(hitTouchingParts) do
		local parentModel = part:FindFirstAncestorOfClass("Model")
		if isInstaceAttackable(part) and not attackedHumanoids[parentModel] then
			attackedHumanoids[parentModel.Humanoid] = true
		end
	end

	-- Damage the humanoids
	for humanoid in pairs(attackedHumanoids) do
		humanoid:TakeDamage(ATTACK_DAMAGE)
	end

	startPosition = maid.instance.PrimaryPart.Position

	wait(ATTACK_STAND_TIME)

	maid.humanoid.WalkSpeed = originalWalkSpeed

	maid.attackAnimation:Stop()

	attacking = false
end
