Pathfinding stuttering

Hello, While my npc is patroling, the pathfinding has no problem, nothing is stuttering or shaking, but when it’s chasing me, it starts to stutter, i was wondering on how to fix that, here’s the video: Watch Hit your Friend! 🥊 - Roblox Studio 2024-12-01 22-50-02 | Streamable

Chasing code:

function FriendsService:ComputeChasingPath(Player: Player, Rig: Model)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	
	local RigHumanoid = Rig:WaitForChild("Humanoid")
	
	RunService.Stepped:Connect(function()
		if not Rig:GetAttribute("ChasingPlayer") then return end
		
		local StartPosition = Rig.PrimaryPart.Position
		local TargetPosition = Character.PrimaryPart.Position
		
		local Success, ErrorMessage = self:SafeComputePath(StartPosition, TargetPosition)
		
		if Success then
			if self.Path.Status == Enum.PathStatus.Success then
				local Waypoints = self.Path:GetWaypoints()
				
				local Distance = (
					Rig.PrimaryPart.Position - Character.PrimaryPart.Position
				).Magnitude
				
				if Distance < Constants["ATTACK_DISTANCE"] and Humanoid.Health > 0 then
					Humanoid:TakeDamage(Constants["CHASING_DAMAGE"])
					
					Rig:SetAttribute("ChasingPlayer", false)
					Player:SetAttribute("Chased", false)
					RigHumanoid.WalkSpeed = Constants["REGULAR_NPC_WALKSPEED"]
				else
					RigHumanoid:MoveTo(Character.PrimaryPart.Position, Character.PrimaryPart)
				end
				
				for _, Waypoint in Waypoints do
					if workspace:GetAttribute("DEBUG") then
						local Part = Instance.new("Part")
						Part.Position = Waypoint.Position
						Part.Color = Color3.fromRGB(255, 255, 0)
						Part.Material = Enum.Material.Neon
						Part.Shape = Enum.PartType.Ball
						Part.Size = Vector3.one * 0.5
						Part.CanCollide = false
						Part.Anchored = true
						Part.Parent = workspace.Map.Trash

						task.delay(0.5, function()
							Part:Destroy()
						end)
					end
					
					if Waypoint.Action == Enum.PathWaypointAction.Jump then
						RigHumanoid.Jump = true
					end

					RigHumanoid:MoveTo(Waypoint.Position, Character.PrimaryPart)
				end
			else
				-- Do nothing if path is not computed, it'll resolve itself

				return
			end
		else
			warn(`Cound't compute Chasing Path, error: {ErrorMessage}`)
			return
		end
	end)
end

Agent parameters

Path = PathfindingService:CreatePath({
		WaypointSpacing = 100,
		AgentCanClimb = true,
		AgentCanJump = false,
		
		PathSettings = {
			["SupportPartialPath"] = true
		}
	}),

Rig.PrimaryPart:SetNetworkOwner(nil)

1 Like

At such short ranges, I think it’d be much less CPU intensive to just use

RigHumanoid:MoveTo(Character.PrimaryPart.Position, Character.PrimaryPart)

to directly move to the player (as you currently do in your code), as what I’m guessing the problem is that the time it takes to compute the next path is slower than the time it takes for the NPC to actually follow the path, meaning it has to pause and wait for the path to be calculated before it can follow you again.

You could probably mix this to make it so it uses that MoveTo as a backup until it calculates the path, when it switches to that until it finishes following the path, before switching back until it computes a new path again.

1 Like

Try turning off support partial path, putting your code in a while loop instead of connecting it to render-stepped so that it computes the path less soften and setting network ownership on all the rig parts to nil. It might just be a client sided thing because it seems as though it only glitches when it gets close. Try checking if the glitching is visible on the server.

Seems like it fixed my issue but im not sure about it yet, I’ll do multiple tests these days and i’ll eventually mark your reply as solution if it fixed

Dunno but seems like Rig.PrimaryPart:SetNetworkOwner(nil) fixed it?