Humanoid:MoveTo() not working?

currently trying to make an npc character that follows slightly behind you, and, i’m having trouble using the MoveTo() function, which, is this, right

i want to use both properties, the vector3 location and the part instance, which i would set as (0, 0, 3) and my player character’s root part
however, if i use MoveTo(), the npc moves rather sporadically depending on what direction i’m facing, yet if i literally set MoveToPart and MoveToPoint separately it works correctly. however, the distance between the player and the npc easily grows in distance as the player walks, which can be very problematic

any help?

1 Like

Hey, it would help us out a lot if you posted your code. This makes it easier for us to understand what you’re attempting to do and spot any errors in your code.

2 Likes

ah, my bad! here it is:

local followie = script.Parent.person.Value --person to be following!
yourself = script.Parent --the model
local yourhumanoid = yourself:WaitForChild("Humanoid")
local them = game.Players:WaitForChild(followie)
print("funny npc script loaded")
local distance = 3
while them and yourhumanoid do
    wait()
    local pos = yourself.HumanoidRootPart.Position
    --print(them:DistanceFromCharacter(pos))
    while them:DistanceFromCharacter(pos) > distance do
        yourhumanoid:MoveTo(Vector3.new(0, 0, distance), them.Character.HumanoidRootPart)
        wait(1)
    end
end

this probably ain’t the best script but it’s something, haha

I might be mistaken but I believe you can only move the character not the humanoid.
If you had the player you would do;

Player.Character:MoveTo()

rather than

Player.Character.Humanoid:MoveTo()

TLDR: change it to
yourcharacter:MoveTo(Vector3.new(0, 0, distance), them.Character.HumanoidRootPart)

1 Like

you actually can use the MoveTo() function on a humanoid, listed here.
it’s all okay, though!

My apologies, I didn’t realize you were trying to make a character walk. I shouldn’t have skimmed through your code. I was having similar issues a few months ago and my research lead to roblox being ‘glitchy’.

1 Like

Can you try your code, but instead of

while them:DistanceFromCharacter(pos) > distance do
    yourhumanoid:MoveTo(Vector3.new(0, 0, distance), them.Character.HumanoidRootPart)
    wait(1)
end

use

if them:DistanceFromCharacter(pos) > distance then
    yourhumanoid:MoveTo(them.Character.HumanoidRootPart.Position)
else
    yourhumanoid:MoveTo(yourself.HumanoidRootPart.Position)
end

That will timeout after 8 seconds if it doesn’t reach its goal.

the NPC just moves right into me, which, isn’t what i intended. i’d like it if they would stand a little bit behind the player, to give them some room to move by themselves before the NPC follows

I just tested this code and it did not time out after 8 seconds

The quoted information is listed here .

My code will update the MoveTo target every time it checks the distance and is therefore unaffected by the 8 second timeout.

Then you’ll want to increase that distance value so the NPC trails farther behind.

even when increased, it doesn’t work as expected. they’ll walk into the player, before stepping away for a moment, still on your tail. i even set it to 6

Try a value higher than 6, I got pretty good results in the 8-12 range.

it’s a little better, but the NPC still takes that weird step away, and it isn’t very smooth, if that makes sense. otherwise, i guess this works, thank you!

Alright, glad to hear that your issue is at least somewhat fixed.

1 Like

There are a few bad practices in your code, but based on the wiki information, your MoveTo should be working fine. However, even when I tested out MoveTo(pos, part) it still wasn’t working correctly.

This code should work as expected, though. Instead of relying on the MoveTo function to find the position directly behind the character, it does it manually. Replace your while loop with this.

while them and yourhumanoid do
	if them.Character then -- make sure the character has loaded
		local root = them.Character.HumanoidRootPart
		local goal = root.CFrame:pointToWorldSpace(distance) -- this basically finds the world coordinates of the position 3 studs behind the character
    
		yourhumanoid:Moveto(goal)
	end
	
    wait(0.5) -- decrease this wait time to reduce choppiness
end

1 Like

Cannibalize this code (Never re-invent the wheel):

1 Like

It only times out if no other calls are made to MoveTo after 8 seconds and the humanoid doesn’t reach its goal. The timer resets when you call it again.

1 Like