✓ Accepted answer
Hi!
I coincidentally have the same script you do, and for some reason it says "game.Workspace:children()". I don't know why that is, but it should be "game.Workspace:GetChildren()".
This is how the script should be.
function findNearestTorso(pos)
local list = workspace:GetChildren()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if temp2:IsA("Model") and temp2.Name ~= script.Parent.Name then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if temp and human and human.Health > 0 then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end end end end
return torso
end
wait(1)
while wait() do
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
Note: Make sure the NPC is unanchored, otherwise it will not follow. Also, for future questions, be sure to code block your code by selecting over it and clicking the Lua button.
4 comments
You're the best man, seriously. I'm glad there is people like you. I hope I can get as good as you someday haha
Although I am curious about one thing, the zombie does follow me now, but it doesn't want to follow me enough to where it attacks me, it only copies my movements
Oh oops, I was fondling with the script in a test server before I gave it to you and I had it where the NPC followed behind you. I have it fixed now where it will directly come in contact with your torso.
What the heck haha, it actually works. I don't know what to say man, I've been looking for this for ages and you answered me within a few minutes. You made my day