How to fix NPC lagging behind player?

I followed DevKing’s tutorial on how to make an NPC follow the player but when I move around no matter how fast the NPC is it can’t catch up to me because it always lags behind so even if the NPC has a walkspeed of 100 it will fail to catch someone with a walkspeed of 16.

local NPCTorso = script.Parent.HumanoidRootPart
local NPCHumanoid = script.Parent.Humanoid

local function FindEnemy()
local AgroDistance = 100
local target = nil
for i,v in pairs (game.Workspace:GetChildren()) do
local human = v:FindFirstChild(“Humanoid”)
local Torso = v:FindFirstChild(“HumanoidRootPart”)
if human and Torso and v ~= script.Parent then
if (NPCTorso.Position - Torso.Position).magnitude < AgroDistance then
target = Torso
if (NPCTorso.Position - Torso.Position).magnitude > AgroDistance then
return
end
end
end
end
return target
end

while true do
wait()
local Torso = FindEnemy()
if Torso then
NPCHumanoid:MoveTo(Torso.Position)

end

end

1 Like

Instead of that use:

local NPCTorso = script.Parent:WaitForChild(¨HumanoidRootPart¨)
local NPCHumanoid = script.Parent:WaitForChild(¨Humanoid¨)
1 Like

Didn’t work, it still lags behind

Can you show a picture of the issue?

https://streamable.com/raabqr
Here’s a video, the NPC had a walkspeed of 32 while I had 16. You can see that its movement is laggy too, this is most likely because of the wait in while true do but if I don’t use that then the game crashes.

Make sure your Humanoid for your character has Collisions off so the NPC can be in your position.

Would that even work? It does move to the player’s location but when the player moves it waits for some time and then moves again.

I tried that and it didn’t change anything

Some have been saying to set :SetNetworkOwner(nil) on the NPC so all physics is handled by server.

1 Like

I tried that too but it just makes it float in a weird way and the problem still exists

I believe the lag is intentional, because the client itself might be lagging. However, this only applies to real game instances and not always in Studio.

Although the aforementioned, you can fix this by replacing the code with something that checks if the NPCs distance, or the player’s distance from the NPC, is close enough to stop and always walk to player’s position until it is close enough. On a trivial side, it’s better to use an event and using RunService’s events to replace wait.

DevKing’s script is bad and ineffective. Here is a better tutorial that you can follow to achieve better results: https://youtu.be/ibvoqnG3YqI

Your enemy NPC lags because he only goes to the last position of the player, this works in conjunction with the wait() that triggers the check and so the enemy will never catch up to you - he does not know where you’re going to go and is always late.

2 Likes

I actually watched that tutorial for like 5 minutes before making this post but then it got too hard and I gave up

Well, just copy what he does then. And if it’s too hard, why even bother doing that in the first place?

If I copy what he does then I won’t learn, and I didn’t do it
Anyways I figured out that if I put “Torso” after Torso.Position then it works a bit better

If you are still looking for an answer, then I suggest doing this:

npchumanoid:MoveTo(Torso.Position + Torso.Velocity)

This will make it so that the npc predicts where you go and goes there.

7 Likes