NPC stutters behind player despite having faster walkspeed

  1. What do you want to achieve? I want the NPC to catch up to the player without any lagging/stuttering

  2. What is the issue? NPC lags/stutters behind player, never catching up, despite having faster walkspeed.

  3. What solutions have you tried so far? I’ve tried replacing while true do with while RS.Heartbeat:Wait() do but that didn’t work. Setting primary part’s network ownership to nil? Doesn’t work. I’ve also tried searching for answers, but that didn’t work as well.

script.Parent.PrimaryPart:SetNetworkOwner(nil)
local CanDamage = true
local RS = game:GetService("RunService")
local root = script.Parent.HumanoidRootPart
local SS = game:GetService("ServerStorage")
local SimplePath = require(SS.SimplePath)

local path = SimplePath.new(script.Parent)

local LastTorso
local function findNearestPlayer(Position)
	task.wait()
	local List = game.Workspace:children()
	local Torso = nil
	local Distance = 999999
	local Temp = nil
	local Human = nil
	local Temp2 = nil
	for x = 1, #List do
		Temp2 = List[x]
		if (Temp2.className == "Model") and (Temp2 ~= script.Parent) then
			Temp = Temp2:findFirstChild("HumanoidRootPart")
			Human = Temp2:FindFirstChildOfClass("Humanoid")
			if (Temp ~= nil) and (Human ~= nil) and (Human.Health > 0) then
				if (Temp.Position - Position).magnitude < Distance then
					Torso = Temp
					Distance = (Temp.Position - Position).magnitude
				end
			end
		end
	end
	return Torso
end
	
local thingything
while true do
	local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		path:Run(target)
		path.Error:Connect(function(errorType)
			if path.Status ~= "Idle" then
				path:Stop()
			end
			script.Parent.Humanoid:MoveTo(target.Position)
		end)
		for i,heh in pairs(script.Parent:GetDescendants()) do
			if heh:IsA("Part") and heh.Name ~= ("HumanoidRootPart" or "Left Leg" or "Right Leg" or "Right Arm" or "Left Arm") then
				thingything = heh
			end
		end
		local e = thingything.Touched:Connect(function(hit)
			if hit.Parent.ClassName ~= ("Accessory" or "Hat") then
				if hit.Parent:FindFirstChildOfClass("Humanoid") then
					hit.Parent:FindFirstChildOfClass("Humanoid").Health = 0
					local torsus = hit.Parent:WaitForChild("Torso")
					torsus.Velocity = script.Parent.HumanoidRootPart.CFrame.LookVector * math.random(100,200)
				end
			end
		end)
		thingything.TouchEnded:Connect(function()
			e:Disconnect()
		end)
	end
end

There is a delay between the client sending the characters position to when the server get the updated position so if your positioning the NPC on the server side it will always be lagging behind

A quick fix would be to change

script.Parent.Humanoid:MoveTo(target.Position)

To

script.Parent.Humanoid:MoveTo(target.Position, target)

That way when the client gets network ownership of the NPC it will be able to move it more accurately

1 Like

Doesn’t help: the NPC is still failing to catch up.

Here is a long thread on the matter:

Also i dont see where in your script you assign network ownership.

1 Like

The issue is that pathfinding updates every second, causing humanoid:MoveTo() loop a few times, making the character to stutter, i recommend check if there is clear line of sight when chasing the player, if there is, just make the humanoid walk to the character position

I’ve assigned network ownership at the very first line of the script.

1 Like

Yes yes you have… this teaches me not to use my mobile as much as i do.

The thread had a solution to the problem I’m facing. Thanks!

1 Like