Roblox NPC MoveTo() Help

Hey people!

I’m using a dummy to follow the player, Now there’s one issue, the dummy always wants to be in the players position, so if u stand he will push you out…

Is there a way to make the dummy follow the player but also make him stop like 2 or 3 studs away from me?

The script:

while true do
		wait(0.5)
		if follow == true then
			dummyHum:MoveTo(player.Character.Torso.Position)
		end
	end

Maybe something like Humanoid:MoveTo(target.Position + Vector3.new(1,0,0))
Don’t know if it works tho, (i can’t test that right now :3)

You can make this script:

  while wait() do
       if follow == true then
             dummyHum.CFrame = player.Character.CFrame
       end
  end

I’m not sure if this will work, as I haven’t tested it in studio and it could be pretty laggy. If this doesn’t work, then you can weld the dummy to the character. This may work better than what I said.

This is also better than MoveTo() because it’s more flexible and allows for you to have more control of the dummy rather than him moving to the player’s humanoid

1 Like

This solution would teleport the dummy to the exact position every second and hence is not a feasible solution.

It is necessary to include the framework MoveTo() or the pathfinding services provided to us by Roblox for optimisation and smoothness clarity.

Hope this helps,
-Tom :slight_smile:

1 Like

Oops. I meant to make the dummy move 2 studs behind the player, not have the exact CFrame.

1 Like

Ok but you’re still manually setting the CFrame of the model which will result in teleports. What happens if the player turns around 180 degrees? He will teleport so he’s still behind you.

The thread is looking for a follow mechanic where the dummy walks behind the player.

1 Like

Yeah that’s correct,

Thomas i know i shouldn’t ask for people to compile scripts but im confused on how to use :ToWorldSpace() or :ToObjectSpace() :sweat_smile:

So essentially you’re going to need to calculate the Vector3 that is 2 studs behind and use it as a parameter of MoveTo()

so like

dummyHum:MoveTo(player.Character.Torso.Position + Vector3.new(2,0,0))

?

torsoCFrame * CFrame.new((humPosition - torsoPosition).Unit * 2)

This should give you a CFrame that is 2 studs away from the target position namely torsoCFrame, index Position for the Vector3 to use.

If it doesn’t work, try making the subtraction operation humPosition - torsoPosition reversed; torsoPosition - humPosition.

2 Likes

It gives me
Position is not a valid member of Humanoid

on the humPosition it gives me the error

humPosition should be set to the HumanoidRootPart Position of your NPC.

torsoPosition should be set to the HumanoidRootPart Position of your target (torsoCFrame.Position).

torsoCFrame should be set to the HumanoidRootPart CFrame of your target.

1 Like

what about the humanoid:MoveTo() what should i put inside since i cant cframe

Ty for the reply btw

But you’ll need to turn it into a Vector3. I think that’s what @ArtFoundation was trying to say in their reply.

To use your original code with these extra bits:

while true do
	wait(0.5)
	if follow == true then
		local playerCframe = player.Character.HumanoidRootPart.CFrame
		local goalCframe = playerCframe * CFrame.new((dummyHum.RootPart.Position - playerCframe.Position).Unit * 2)
		dummyHum:MoveTo(goalCframe.Position)
	end
end

I changed out a couple variable names to make it a little clearer.

1 Like

I’m home now and tested this, turns out the dummy still doesn’t stop 2 studs behind the player, he just kinda goes around you, pushes u out of the way etc…

https://gyazo.com/0587b87b7f755cbfc964c1b9a4e290e7

It should be 2 studs from centre to centre, if that happens to be at the side of you and you meet at angles that will probably still result in some pushing.

Perhaps increase it slightly and/or put some sort of condition where if you’re within a slightly larger radius it doesn’t update the MoveTo.

Something like this maybe:

while true do
	wait(0.5)
	if follow == true then
		local playerCframe = player.Character.HumanoidRootPart.CFrame
		if (dummyHum.RootPart.Position - playerCframe.Position).Magnitude > 2.5 then
			local goalCframe = playerCframe * CFrame.new((dummyHum.RootPart.Position - playerCframe.Position).Unit * 2)
			dummyHum:MoveTo(goalCframe.Position)
		end
	end
end
1 Like

Thanks for the script!

I’ve tried it and the npc was in front of me so i rotated the subtraction dummyhum - hrp to hrp - dummyhum, and now he’s always on my side or a little to the front

still didn’t get the effect of him being behind me ._.

Thank you for your help tho, if you want to continue trying to find the answer then sure

If you want him to always be behind you, then the script is a bit different - apologies if we misunderstood, but the solution I incorporated was to keep 2 studs away in the direction the NPC was heading towards the player.

If instead you want it to always be behind the player, then you can swap out the goalCframe line with the following:

local goalCframe = playerCframe - playerCframe.LookVector * 2

This uses the player’s HRP’s CFrame.LookVector to determine the way you’re facing, and move -2 multiplied by that to go 2 studs behind.

1 Like

I just thought of it more and more for no reason, and i thought that i can make a part on the player behind him and the npc will move to the part