I am sorry for the late response, I was testing your code with my friend. Zombie works well, but it still chases the first person, who joined the server and I can’t even understand why does it happen.
Yeah I’m sorry I made a mistake. It’s not constantly searching for the closest torso.
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 wait() do
FindNearestTorso()
if target ~= nil then
wait(math.random(1,5))
zombieHumanoid:MoveTo(target.Position, target)
end
end
This should work, however it’s pretty expensive when it comes to performance so you might wanna create a folder in the workspace and put all the characters that can be chased by the zombie, then instead of doing game.Workspace:GetChildren()
do game.Workspace.FolderNameHere:GetChildren()
and use in ipairs
instead of in pairs
.
local zombie = script.Parent
local zombieHumanoid = zombie.Humanoid
local zombieTorso = zombie.Torso
local distance = 200
local function FindNearestTorso()
local closest
local closestPos = 0
for _,v in pairs(game.Workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v ~= zombie then
if closest == nil then
closest = v
closestPos = (v.HumanoidRootPart.Position - zombieTorso.Position).Magnitude
else
local dis = (v.HumanoidRootPart.Position - zombieTorso.Position).Magnitude
if closestPos > dis then
closest = v
closestPos = dis
end
end
end
end
end
while wait() do
target = FindNearestTorso()
if target then
repeat
wait(math.random(1,5))
zombieHumanoid:MoveTo(target.Position, target)
until (target.HumanoidRootPart.Position - zombieTorso.Position).Magnitude < distance or target == nil
end
end
Much thanks! Zombie had some strange behaviour while deciding who will be his target, so I’ve made some additions and it now works as I need him to.
Also much thanks for advice about a folder for players and using ipairs, I will do it as soon as possible!
This script works as well, much thanks!
No problem, glad I could help!
Nevertheless, the script didn’t work so well. Zombies were just staying still after character’s death or they were focusing only one character again. So I’ve spent several hours in order to figure out what is the essence of this strange bug. And, well, I did it through several thousand tests lol
So the fully working code looks like this
local zombie = script.Parent
local zombieHumanoid = zombie.Humanoid
local zombieTorso = zombie.Torso
local distance = 300
local target = nil
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:FindFirstChild("LowerTorso") and (v.LowerTorso.Position - zombieTorso.Position).Magnitude < aggroDistance then
target = v.LowerTorso
end
else
if v:FindFirstChild("LowerTorso") and (v.LowerTorso.Position - zombieTorso.Position).Magnitude < aggroDistance then
target = v.LowerTorso
aggroDistance = (v.LowerTorso.Position - zombieTorso.Position).magnitude
end
if target ~= nil and target.Parent.Humanoid.Health <= 0 then
target = nil
break
end
end
end
end
return target
end
while wait() do
local human = FindNearestTorso()
if human ~= nil and (human.Position - zombieTorso.Position).magnitude < distance then
zombieHumanoid:MoveTo(human.Position, human)
end
end
Upd:
I do remember about HumanoidRootPart, but I am using LowerTorso for 2 reasons:
- This is due to specifics of other scripts;
- In my game only R15 avatars allowed.
Oh yes, I know why. I didn’t make it so it searched for a new target when the character the zombie is chasing dies. Good job with fixing it! As I told you I didn’t test it in Studio so I was sure there would’ve been some bug.
Anyway your code was very close to what I needed. I appreciate your help and advice