I have succesfully made an NPC move to a path using MoveTo().
Now I want the NPC to keep moving using MoveTo() but to ignore this path and instead follow the user if the user is nearby.
How is this possible and efficient?
Just store your moveto values (the part and the position) in 2 variables just before you start following a player and put it back when you stop following (player moves out of range)?
Then you can use the player as an object to follow using Humanoid:MoveTo which makes your life a lot better to program that part.
If the reason you want to keep the original target in the humanoid, is because other scripts modify this target while you ignore it, then you will have to make an ObjectValue (say as a child of humanoid) or something that stores the target that these scripts can interact with.
Then the script that checks which target to follow connects a function to the Changed event of that objectvalue, and if not following a player, sets the new target using Humanoid:moveTo()
That way, it's easy to ignore the original target while always storing it in a safe place.
Example implementation (untested, just for example):
local humanoid = --find player's humanoid that you want to move local objectValue = Instance.new("ObjectValue", humanoid) objectValue.Value = --find original target to walk to local following = false objectValue.Changed:Connect(function(newVal) if not following then humanoid:MoveTo(Vector3.new(), newVal) end end) while true do local nearestPlayer = playerInRange() if nearestPlayer and nearestPlayer:FindFirstChild("Character") and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then if not following then following = true end if not (humanoid.WalkToPart == nearestPlayer.Character.HumanoidRootPart) then humanoid:MoveTo(Vector3.new(), nearestPlayer.Character.HumanoidRootPart) end else if following then following = false humanoid:MoveTo(Vector3.new(), objectValue.Value) end end wait(0.5) end
(of course add a function playerInRange() that returns the nearest player or nil if none in range)