Hello! I’m trying to make a Zombie script where the Zombie follows the nearest target.
I don’t get any errors and everything prints out but it doesn’t follow me though.
Don’t worry, I enabled R6 so there is a Torso.
local zombieModel = script.Parent
local Hum = zombieModel.Humanoid
local zombieTorso = zombieModel.Torso
local AggroDistance = 200
local function SearchForTarget()
for _,v in pairs(game.Workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= Hum then
print("Hum Found which is not the Zombie")
--Distance
if (v.Torso.Position - zombieTorso.Position).magnitude < AggroDistance then
print("Distance check")
--New Aggrodistance + Follow player
AggroDistance = (v:FindFirstChild("Torso").Position - zombieTorso.Position).magnitude
print("New Aggrodistance set")
return v:FindFirstChild("Torso") --Returns the position of the target
end
end
end
end
--Game loop
while wait(1) do
print("GameLoop")
local Target = SearchForTarget() --Gets the returned value (Torso.Position)
if Target then
print("Target is not nil") --If its not nil
Hum:MoveTo(Target.Position)
print("Moves to Target")
end
end
It’s the same as Torso. TheDevKing made this and it works. I wanted to recreate it without looking at his script.
His Script:
local ZombieTorso = script.Parent.Torso
local ZombieHumanoid = script.Parent.Humanoid
local function findTarget()
local agroDistance = 100
local target = nil
for i,v in pairs(game.Workspace:GetChildren()) do
local human = v:FindFirstChild("Humanoid")
local torso = v:FindFirstChild("Torso")
if human and torso and v ~= script.Parent then
if (ZombieTorso.Position - torso.Position).magnitude < agroDistance then --checks the distance
agroDistance = (ZombieTorso.Position - torso.Position).magnitude --Sets new agrodistance, so if someone else is closer, it follows him
target = torso --Sets the target, because target was nil before
end
end
end
return target
end
while wait(1) do
local torso = findTarget() --torso will be nil, as its the same as target so if torso is not nil< then do something
if torso then
ZombieHumanoid:MoveTo(torso.Position)
end
end```
Yep, I want to recreate it without looking at this script. I’m actually done with all the TheDevKing tutorials so I wanted to make something that he made before bc it’s easier to compare our scripts like that and see what I did wrong.
local zombieModel = script.Parent
local Hum = zombieModel.Humanoid
local zombieTorso = zombieModel.Torso
local zombieHumanoidRootPart = zombieModel.Hum
local AggroDistance = 200
local function SearchForTarget()
for _,v in pairs(game.Workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and v:FindFirstChild("Humanoid") ~= Hum then
print("Hum Found which is not the Zombie")
--Distance
if (v.Torso.Position - zombieTorso.Position).magnitude < AggroDistance then
print("Distance check")
--New Aggrodistance + Follow player
AggroDistance = (v:FindFirstChild("Torso").Position - zombieTorso.Position).magnitude
print("New Aggrodistance set")
return v:FindFirstChild("Torso") --Returns the position of the target
end
end
end
end
--Game loop
while wait(1) do
print("GameLoop")
local Target = SearchForTarget() --Gets the returned value (Torso.Position)
if Target then
print(Target.Parent) --If its not nil
Hum:MoveTo(Target.Position)
print("Moves to Target")
else
zombieTorso:MoveTo(ZombieTorso.Position + Vector3.new(math.random(-50,50), 0, math.random(-50,50))
end
end
Alright, I found the solution. It was the Zombie Model’s fault. I changed it and now and TheDevKing’s code works. His code didn’t work on the old Zombie aswell. Thank you.
Need an extra closing bracket at the end. Glad you solved it! Free Model scripts will often conflict or interfere with your own so be sure to remove them.