Pathfinding AI stuttersteps while following player

The NPC stutters and eventually stops moving with PathfindingService

Script:

local function moveToPoint(enemy,Modde)
	if Modde == "Defense" then
		local success, errorMessage = pcall(function()
			Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position - CFrame.new(char.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position).LookVector * 8 )
		end)
		
		if success and Path.Status == Enum.PathStatus.Success then
			wayPoints = Path:GetWaypoints()
			blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
				if blockedWaypoint >= nextWaypoint then
					blockedConn:Disconnect()
					moveToPoint(enemy)
				end
			end)
			if not reachedConn then
				reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
					if reached and nextWaypoint < #wayPoints then
						nextWaypoint += 1
						char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
					else
						reachedConn:Disconnect()
						blockedConn:Disconnect()
					end
				end)
			end
			nextWaypoint = 2
			char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
		else
			warn("Path Blocked!",errorMessage)
		end
	elseif Modde == "Offense" then
		local success, errorMessage = pcall(function()
			Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position - CFrame.new(char.HumanoidRootPart.Position, enemy.HumanoidRootPart.Position).LookVector * 2.5 )
		end)	
		
		if success and Path.Status == Enum.PathStatus.Success then
			wayPoints = Path:GetWaypoints()
			blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
				if blockedWaypoint >= nextWaypoint then
					blockedConn:Disconnect()
					moveToPoint(enemy)
				end
			end)
			if not reachedConn then
				reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
					if reached and nextWaypoint < #wayPoints then
						nextWaypoint += 1
						char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
					else
						reachedConn:Disconnect()
						blockedConn:Disconnect()
					end
				end)
			end
			nextWaypoint = 2
			char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
		else
			warn("Path Blocked!",errorMessage)
		end
	elseif Modde == "None" then
		local success, errorMessage = pcall(function()
			Path:ComputeAsync(char.PrimaryPart.Position, enemy.HumanoidRootPart.Position)
		end)	

		if success and Path.Status == Enum.PathStatus.Success then
			wayPoints = Path:GetWaypoints()
			blockedConn = Path.Blocked:Connect(function(blockedWaypoint)
				if blockedWaypoint >= nextWaypoint then
					blockedConn:Disconnect()
					moveToPoint(enemy)
				end
			end)
			if not reachedConn then
				reachedConn = char.Humanoid.MoveToFinished:Connect(function(reached)
					if reached and nextWaypoint < #wayPoints then
						nextWaypoint += 1
						char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
					else
						reachedConn:Disconnect()
						blockedConn:Disconnect()
					end
				end)
			end
			nextWaypoint = 2
			char.Humanoid:MoveTo(wayPoints[nextWaypoint].Position)
		else
			warn("Path Blocked!",errorMessage)
		end
	end
end

while true do
    if not movementCD then
				if lastMode == nil or "Offense" then
					local moveDecider = math.random(1,2)
					if moveDecider == 1 then
						mode = "Defense"
						lastMode = mode
						moveToPoint(enemy,mode)
						coroutine.wrap(function()
							task.wait(5)
							movementCD = false
						end)()
					elseif moveDecider == 2 then
						mode = "Offense"
						lastMode = mode
						moveToPoint(enemy,mode)
						coroutine.wrap(function()
							task.wait(5)
							movementCD = false
						end)()
					end
				elseif lastMode == "Defense" then
					mode = "Offense"
					lastMode = mode
					moveToPoint(enemy,mode)
					coroutine.wrap(function()
						task.wait(5)
						movementCD = false
					end)()
				end
			end
            task.wait(0.1)
        end

1 Like

Bump. Still need an answer to my question.

Google searches usually help

1 Like

this part?
change it to just task.wait() or a lower value like 0.03

This isn’t a network issue at all. The problem is that the NPC becomes increasingly slower as it makes new paths every time. Lagging/stuttering has already been fixed. It’s just that the walking becomes weirder by the second.

task.wait() seems to be faster than wait() by a lot. Using task.wait() lags the server considerably more than task.wait(0.1). Last time I used task.wait(), script performance jumped up to 23-24% for 1 NPC.

Added connections to the functions to disconnect them whenever the NPC is about to run another path.

My dear friend! Tell me, has the script that’s on top already been fixed? Thank you so much!:black_heart: I spent two days to find a solution​:sob:

It has been a really long time and the script has changed drastically since then, so I cannot give you an definite solution anymore.