Roblox Pathfinding Lagging/Stuttering

  1. What do you want to achieve?
    A want to let the dog follow the player in a smooth way.

  2. What is the issue?
    The pathfinding is stuttering like it is lagging.

  3. What solutions have you tried so far?
    Multiple ways of making the path regenerate when moved.

2 Likes

You should send your code so people can help you diagnose it properly.

4 Likes

Are you using a server script to do it or a local script?

1 Like

I am using one server script inside the dog.

Script:

local PathService = game:GetService("PathfindingService")
local Path = PathService:CreatePath()
local WalkAnimation = script.Parent.Humanoid:LoadAnimation(script.WalkAnim)

for Index, Limb in pairs(script.Parent:GetChildren()) do
	if Limb:IsA("BasePart") then
		Limb.Anchored = false
		if Limb ~= script.Parent.PrimaryPart then
			Limb.CanCollide = false
		else
			Limb.CanCollide = true
		end
	end
end

function FindPath(Player)
	local FindPathCourotine = coroutine.wrap(function()
		local Character = Player.Character or Player.CharacterAdded:Wait()
		
		while true do
			Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, Character.HumanoidRootPart.Position)
			
			for Index, Point in pairs(Path:GetWaypoints()) do
				if Point.Action == Enum.PathWaypointAction.Jump then
					script.Parent.Humanoid.Jump = true
					repeat 
						wait()
					until script.Parent.Humanoid.FloorMaterial ~= nil
				end
				
				script.Parent.Humanoid:MoveTo(Point.Position)
				
				local MoveStatus = script.Parent.Humanoid.MoveToFinished:Wait(2)
				
				if not MoveStatus then
					FindPath(Player)
					return
				end
			end
			
			wait()
		end
	end)
	
	FindPathCourotine()
end

script.Parent.Vest.ClickDetector.MouseClick:Connect(function(Player)
	script.Parent.Vest.ClickDetector.MaxActivationDistance = 0
	FindPath(Player)
end)

Can you find the problem? Because i have been searching for a solution for a while now and can’t get it to work smoothly.

Try setting the network ownership of the npc to the server, it can cause stuttering if its being replicated by the client sometimes.

13 Likes

I do not think the problem is network-related, I think your code is just slow.

You are doing this:

  1. Compute path (this takes some time)
  2. Jump if necessary (busy wait while doing this), then move to the next point.
  3. Wait until we reach the next point
  4. If we did, go to step 2
  5. If we did not within 2 seconds, go to step 1

Instead of all this waiting, use event-based code to properly deal with reaching waypoints, blocked waypoints, etc. Read this great article that goes over how to do this properly, and make some improvements to your code :slight_smile:

Also, read this devforum post for more motivation about using events, rather than busy waits (like repeat wait() until ...)

EDIT: The article I linked handles blocked paths, but not a constantly moving goal. However, having a system like that which avoids a for loop would still be useful for solving that problem.

1 Like

Okey i will take a look into the article you send! If it works i wil mark your post as the solution.

It works but how do i now put this into a loop so it contantly follows me around?

**Script right now: **

local PathService = game:GetService("PathfindingService")
local Path = PathService:CreatePath()

local DogUser
local Waypoints
local CurruntWaypoint

local function StartPath()
	Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, DogUser.Character.HumanoidRootPart.Position)
	Waypoints = {}
	
	if Path.Status == Enum.PathStatus.Success then
		Waypoints = Path:GetWaypoints()
		CurruntWaypoint = 1
		script.Parent.Humanoid:MoveTo(Waypoints[CurruntWaypoint].Position)
	else
		script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
	end
end

local function OnWaypointChecked(Checked)
	if Checked and CurruntWaypoint < #Waypoints then
		CurruntWaypoint = CurruntWaypoint + 1
		script.Parent.Humanoid:MoveTo(Waypoints[CurruntWaypoint].Position)
	end
end

local function OnWaypointBlocked(BlockedWaypoint)
	if BlockedWaypoint > CurruntWaypoint then
		StartPath()
	end
end

Path.Blocked:Connect(OnWaypointChecked)
script.Parent.Humanoid.MoveToFinished:Connect(OnWaypointChecked)

script.Parent.Vest.ClickDetector.MouseClick:Connect(function(Player)
	script.Parent.Vest.ClickDetector.MaxActivationDistance = 0
	print("Activating!")
	DogUser = Player
	StartPath()
end)

This sorta solved the problem. It made the stuttering way less!

Glad to hear, I had similar issues while working on npcs as well, and when I did that they were way more stable so I figured it may be the case.

2 Likes

By the way I found a typo, CurruntWaypoint should be CurrentWaypoint @Dutch_Coder