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!
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
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.
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