Laggy NPC movement (towards player)

yeah pretty much, but you could also use Humanoid.MoveDirection

1 Like

It’s “laggy” because of the MoveToFinished:Wait() call. Essentially what’s happening is you’re giving the humanoid a target position to move to, and then pausing the code while the humanoid walks to the position. While the humanoid is walking, the player is also moving, but the humanoid is continuing to walk to the original player position and doesn’t start walking to the player’s current position until after it has reached the old position.

In the GIF you posted with the MoveToFinished:Wait() removed, it doesn’t appear to be laggy and the humanoid is following the player much more closely, why do you say it is?

The gif is in about 20 fps, i’ll try to get a better shot.
What I mean is this:
https://gyazo.com/d2987ce04b0110610bcee9756c633862.gif
You see the health bar is being choppy, that’s the movement, anyway to fix that as well?

Something that I find very weird is when the

Humanoid:MoveTo()

is called the humanoid doesn’t like it if it’s only given a position. Try doing:

Humanoid:MoveTo(Target.Character.PrimaryPart.Position,Target.Character.PrimaryPart)
Humanoid.MoveToFinished:Wait()

You can also add a number in the Wait() so that the humanoid doesn’t wait for it to go to one position for over 7 seconds.

1 Like

let’s suppose this Red part is the target location, while using Humanoid.MoveDirection we can get a decent result so that the NPC is following ahead of the player

I think you’ll have to post the game, because I don’t see any unusual lag in the GIF.

That seems to randomly freeze the NPC after I jump. (and he starts moving again after I jump later)

https://gyazo.com/47fe9b95cea4d0f5c7b9f7e14b91f703

In my playtest it looked like the NPC was trying to predict the movement of the player :joy:
Essentially, it does work.
https://gyazo.com/72cbcc0ef5ec1c86e2ca2f26f8c86480.gif

at least the NPC is now smart :wink:

2 Likes

Get rid of the Y position. The humanoid doesn’t move to something higher than it’s humanoid root part. Or at least that’s what I see.

How can I get rid of the Y position? I just have the PrimaryPart, from the code you sent here.

Vector3 * Vector3.new(1, 0, 1)

I think you misunderstand what connecting a wait() to an event (MoveToFinished) does. It doesn’t cancel the movement if the time inside the wait() has passed, it yields the thread until the event fires and the number inside the wait() is an additional time to wait after the event fires. So in this case, if you put a MoveToFinished:wait(7) then when the humanoid reaches it’s destination it’s going to stand still for 7 seconds before going back to following.

Nevermind, I assumed that the event:wait() followed the same functionality as a normal wait(), but it simply yields until the event fires (and doesn’t accept any parameters).

Break up the positions

Vector3.new(Target.Character.PrimaryPart.Position.X,0.5,Target.Character.PrimaryPart.Position.Z)

Uh, this seems to make it worse… (Woah, apologize for the gif quality…)
https://gyazo.com/28259776c93f9722dc0d10fea143ff25.gif

Try this;

while true do
	local Target = GetClosestPlayer(400)
	print(Target)
	if Target ~= nil then
		if Target.Character ~= nil then
			Humanoid:MoveTo(Target.Character.PrimaryPart.Position)
			Humanoid.MoveToFinished:Wait()
		end
	else
		wait()
	end
end

Now we should only call a wait call once.
However, you are yeilding the next loop iteration until after either a wait() or a MoveToFinished:Wait().
Try this, and then see how it works, you may experience less stutter, but I think there will still be some.

3 Likes

I may be wrong or I may NOT be wrong, but I just encountered this problem. I noticed that it only happens when you get within a certain vicinity so it could be an issue with replication to client, etc. I solved it by iterating through the NPC, getting all BasePart’s, and doing BasePart:SetNetworkOwner(nil) to make the Server the network owner for the part. This eliminated the laggy choppy movement it used to have.

23 Likes

I wasn’t on ROBLOX dev for over a month but i’ll try this ;b

Funnily enough, this fixed the entire issue of this post and I somehow didn’t realize myself back then that this was an issue :slight_smile:

2 Likes

I’m very late to this thread but thank you so much!