Zombies chase only one person

Hello! I have a classic zombie place and at the very beginning of development, I had inserted some classic zombies from Free Models as Placeholders for the future zombies of mine.

But a few days ago I faced a problem, when a zombie chases only one person. To solve the problem I’ve taken DevKing’s script and slightly modified it, so it looks like this:

local zombie = script.Parent
local zombieHumanoid = zombie.Humanoid
local zombieTorso = zombie.Torso
local distance = 200

local function FindNearestTorso()
local aggroDistance = distance
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= zombieHumanoid then
			if (v.LowerTorso.Position - zombieTorso.Position).magnitude < aggroDistance then 
				aggroDistance = (v:FindFirstChild("LowerTorso").Position - zombieTorso.Position).magnitude 
				return v:FindFirstChild("Torso") --Returns the position of the target
				end
			end
		end
end

while wait (math.random(1,5)) do
	local target = FindNearestTorso() 
	if target ~= nil then
		zombieHumanoid:MoveTo(target.Position, target) 
	end
end

I’ve inserted a simple R15 Dummy to test out new Zombie AI, but that didn’t solve the issue. Zombie is chasing the Dummy even when I am closer to him. It just doesn’t pay any attention to me.

Then I deleted the Dummy and asked my friend to test Zombie AI with me. I joined my game first, my friend joined second. Zombie was chasing me even when the friend was closer. When I left the game zombie started to chase my friend, but when I rejoined zombie suddenly started to chase me again.
Also I want to note, that this bug appeared after I had put the zombie into a folder to organise Workspace. Reverse action doesn’t solve the issue.
Any help is appreciated!

That’s because the FindNearestTorso function keeps on returning when it finds a close enough humanoid, so it doesn’t check the others.

local zombie = script.Parent
local zombieHumanoid = zombie.Humanoid
local zombieTorso = zombie.Torso
local distance = 200

local function FindNearestTorso()
local aggroDistance = distance
local target = nil
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= zombieHumanoid then
			if (v.LowerTorso.Position - zombieTorso.Position).magnitude < aggroDistance then 
				aggroDistance = (v:FindFirstChild("LowerTorso").Position - zombieTorso.Position).magnitude 
				target = v.LowerTorso
			end
		end
	end
	return target
end

while wait(math.random(1,5)) do
	local target = FindNearestTorso() 
	if target ~= nil then
		zombieHumanoid:MoveTo(target.Position, target) 
	end
end
2 Likes

Why LowerTorso instead of PrimaryPart?

He’s using R15 and that is his preference.

Oh oops my bad. I was confused cause I usually see people using the HumanoidRootPart or simply the PrimaryPart.

It’s better to return HumanoidRootPart tbh because it support both R15 and R6

1 Like

I am sorry, but this code doesn’t work properly. Now the zombie just travels to target.Position and doesn’t move anywhere when he reaches its destination.

Oh, thank you for advice, I will use HumanoidRootPart instead

Because you coded the script to move to the target position and not anywhere else…

But the first version of this script (you can find it inside classic zombies) uses the same mechanism and it’s still working as intended. :thinking:

What exactly is the problem? The zombie stop moving?

Try removing the math.random(1,5) and just do wait()

Yes, it reaches its destination (i.e. the very first Position of the character’s lower torso) and then stops moving.

Upd:

Try removing the math.random(1,5) and just do wait()

Nothing changes

Good idea, the zombie should lose interest of the player when It’s out of range.

I didn’t set the distance loll

oh, lol. Even if It’s not what you meant, It’s still not a bad idea.

What is this and how to do this?

I just wrote this script for you, I didn’t test it in Studio yet so if you find any problem let me know

local zombie = script.Parent
local zombieHumanoid = zombie.Humanoid
local zombieTorso = zombie.Torso
local distance = 200
local target = nil

local function FindNearestTorso()
	local aggroDistance = distance
	for i, v in pairs(game.Workspace:GetChildren()) do
		if v:FindFirstChild("Humanoid") and v ~= zombie then
			if target == nil then
				if (v.PrimaryPart.Position - zombieTorso.Position).Magnitude < aggroDistance then 
					target = v.PrimaryPart
				end
			else
				if (target.Position - zombieTorso.Position).Magnitude > (v.PrimaryPart.Position - zombieTorso.Position).Magnitude then
					target = v.PrimaryPart
				end
			end
		end
	end
end

while game:GetService("RunService").Heartbeat:Wait() do
	if target ~= nil then
		wait(math.random(1,5))
		zombieHumanoid:MoveTo(target.Position, target) 
	else
		FindNearestTorso()
	end
end
2 Likes

That’s not part of the script, that’s because to write a topic or an answer on the forum you have to reach at least 30 letters lmao.