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)
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.
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.
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