How to make a model that chase players if they got in the red zone

I want a model that will chase players if they are inside the red zone and it will stop chasing if they are outside the red zone, like this :
image

I have tried this script but the problem is it chase every nearby humanoid, which making no fun
script :

local enemy = script.Parent
local larm = enemy:FindFirstChild("Left Arm")
local rarm = enemy:FindFirstChild("Right Arm")

--enemy.PrimaryPart:SetNetworkOwner(nil) --Uncomment if you have a primary part

function findNearestTorso(pos)
	local list = game:GetService("Players"):GetPlayers()
	local torso = nil
	local dist = .1
	local temp = nil
	for _,player in pairs(list) do
		local char = player.Character
		local rootPart = char and char:FindFirstChild("HumanoidRootPart")
		if not rootPart then continue end
		local distance = (rootPart.Position - pos).Magnitude
		if distance >= dist then continue end
		torso = rootPart
		dist = distance
	end
	return torso
end

while true do
	wait(0.1)
	local target = findNearestTorso(enemy.Torso.Position)
	if target == nil then continue end
	enemy.Zombie:MoveTo(target.Position, target)
end

I’m not sure, but this might help.

1 Like