I'm trying to make an NPC move only if the character is within 10 studs of the NPC using magnitude and if it isn't then for it to wait until the player is within 10 studs. Any help? Here is my script.
for i,v in pairs(game.Players:GetChildren()) do if v and v.Character then local distance = 10 while true do wait() if (script.Parent.UpperTorso.Position - v.Character.UpperTorso.Position).magnitude <= distance then script.Parent.Humanoid:MoveTo(v.Character.UpperTorso.Position) else repeat wait() until (script.Parent.UpperTorso.Position - v.Character.UpperTorso.Position).magnitude <= distance end end end end