I want the npc to stop near the player
i tried to do if (torso.Position - enemy.Torso.Position).Magnitude >= 5 then
which makes the enemy go back and forth
local enemy = script.Parent
local player = game.Players
function findplyr()
for i,v in pairs(player:GetPlayers()) do
print(v)
if v.Character then
local torso = v.Character.Torso
if (torso.Position - enemy.Torso.Position).Magnitude <= 20 then
print("close")
enemy.Humanoid:MoveTo(torso.Position)
else
enemy.Humanoid:MoveTo(Vector3.new(-7.2, 3, -44.5))
print("far")
end
end
end
end
game:GetService("RunService").Heartbeat:Connect(function()
print("running")
findplyr()
end)```