In a script I'm working on, an NPC should move to the nearest player. However, in the current script I have, the NPC doesn't move at all. But, if I change line 42 to:
script.Parent:MoveTo(target.Position,target)
it directly teleports the NPC to the target player. But with the current line, the NPC doesn't gradually move towards the player. What am I doing wrong?
function findNearestTorso(pos) local list = game.Workspace:children() local torso = nil local dist = 10000 local temp = nil local human = nil local temp2 = nil for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) then temp = temp2:findFirstChild("HumanoidRootPart") human = temp2:findFirstChild("Humanoid") if (temp ~= nil) and (human ~= nil) 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 while true do wait(1) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position,target) --this line print("moveto") end end
Thanks!
:MoveTo() is basically a teleport for models like changing the position. if you want the NPC to go somewhere by walking use Humanoid.WalkToPart or Humanoid.WalkToPoint