Problem with MoveTo and pathfinding service to create an attacking NPC that chases a player

I’m currently working to create an NPC that chases and attacks players- for the most part, it’s working fine. Though when the NPC has a faster walkspeed than my character I get a sort of laggy result as seen blow:

This is the part of my script that handles the tracking and attacking:

	local function makePath()
		local newPath = PathFindingService:CreatePath()
		newPath:ComputeAsync(Character.HumanoidRootPart.Position,target.Position)
		local waypoints = newPath:GetWaypoints()
		
		if self.DebugMode then
			for _,Point in pairs(waypoints) do
				local t = {
					Size = Vector3.new(1,1,1),
					Anchored = true,
					CanCollide = false,
					Material = Enum.Material.Neon,
					["CFrame"] = Point.CFrame,
				}
				local newPart = makeDebugPart(t)
				delay(3,function()
					newPart:Destroy()
				end)
			end
		end
		
		if newPath.Status == Enum.PathStatus.Success then
			print("made path")
			for _,Point in pairs(waypoints) do
				if self.Target == nil then
					break
				end
				if Point.Action == Enum.PathWaypointAction.Jump then
					Humanoid.Jump = true
				end
				Humanoid:MoveTo(Point.Position)
				local timeout = Humanoid.MoveToFinished:Wait(1)
				if not timeout then
					Humanoid.Jump = true
					makePath()
					break
				end
				if checkView() then
					repeat
						Humanoid:MoveTo(target.Position)
						print(self.Target,self.Disabled,getDist())
						if self.Target and not self.Disabled and getDist() <= 5 then
							print("hit")
						end
						wait(0.1)
						if not self.Target then
							break
						end
					until not checkView()
					print("lost view")
					if persist and self.Target and not self.Disabled then
						makePath()
					end
					break
				end
				if (target.Position-waypoints[#waypoints].Position).Magnitude > 20 then
					makePath()
					break
				end
			end
		else
			print("failed to make path")
			makePath()
		end
	end

	-- core
	coroutine.wrap(function()
		self.Target = target
		makePath()
	end)()

Any help would be much appreciated!

Could it be that the server is detecting that the NPC is actually hitting you repeatedly and there is a delay? That would explain why it looks bumpy.

1 Like

Remove the number “1” in the line above. The MoveToFinished event on it’s own will wait for whatever duration is required up to a maximum of 8 seconds. Adding a value in there can override an in progress move.

Also, does the jittery movement only occur when the NPC gets close to your character? If yes, then have you set:
<your NPC>:SetNetworkOwner(nil)

1 Like

Had the same problem and this was the solution:

YourBot.PrimaryPart:SetNetworkOwner(nil)

2 Likes

Bro Thanks So Much! This helps solve so many of my problems!