Help with Pathfinding Stutter

What am I trying to do?

Im trying to make a Advanced AI Chase with SimplePath

AI Function

The AI’s function is as follows:

  • When the AI’s Raycast hits the player directly, stop the SimplePath and start walking straight towards the player (for a smoother path)

  • When the Raycast hits a wall and not towards the player, activate the SimplePath

What is the problem?

My problem is that every time the NPC’s RootPart gets anchored and then unanchored, the NPC starts stuttering every time it moves a point along the path.

Solutions I tried:

I tried to make a loop where every time a part gets unanchored, it sets its NetworkOwner to nil. But I realized that the problem doesn’t seem to be that (or maybe it is).

AI Script (Using SimplePath):

--(( Loaded Modules ))--
local SimplePath = require(script.Modules:WaitForChild("SimplePath"))

--(( Services ))--
local Players = game:GetService('Players')
local PathfindingService = game:GetService("PathfindingService")
local RaycastFilterType = Enum.RaycastFilterType
local LastTick = 0

--(( Strings & Values ))--
local Debounce = false
local Humanoid = script.Parent:FindFirstChildOfClass("Humanoid")

--(( Check Network Function ))--
local function CheckNetwork()
	while true do
		for _, v in script.Parent:GetDescendants() do
			if v:IsA("BasePart") or v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
				if v.Anchored == false then
					local NetworkOwner = v:GetNetworkOwner()
					
					if NetworkOwner ~= nil then
						v:SetNetworkOwner(nil)
					end
				end
			end
		end
		
		wait(1)
	end
end

--(( Start Function ))--
coroutine.wrap(CheckNetwork)() 


--(( Raycast Variables ))--
local Raycast = RaycastParams.new()
Raycast.FilterDescendantsInstances = {script.Parent}
Raycast.FilterType = RaycastFilterType.Exclude
Raycast.RespectCanCollide = false
Raycast.IgnoreWater = true

--(( Internal Functions ))--
local function RayCast(User: Player, Debug)
	local UserPosition = User.Character.PrimaryPart.Position
	local Direction = (UserPosition - script.Parent.HumanoidRootPart.Position).Unit * 10000
	local RaycastResult = workspace:Raycast(script.Parent.HumanoidRootPart.Position, Direction, Raycast)

	if RaycastResult and RaycastResult.Instance:IsDescendantOf(User.Character) then
		local DotProduct = Direction:Dot(script.Parent.HumanoidRootPart.CFrame.LookVector)
		if DotProduct > (Debug or 0.1) then
			return User
		end
	end

	return false
end

local path = SimplePath.new(script.Parent)
path.VisualizeWaypoints = true

spawn(function()
	while true do
		task.wait()
		local closestPlayer, closestDistance = nil, math.huge

		for _, player in pairs(Players:GetPlayers()) do
			if player.Character ~= nil  then
				local distance = (script.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
				local Humanoid = player.Character:FindFirstChild("Humanoid")

				if distance < closestDistance then
					closestPlayer = player
					closestDistance = distance
				end
			end
		end

		if closestPlayer ~= nil then
			if closestPlayer and closestPlayer.Character then
				if not closestPlayer.Character:FindFirstChild("Enemy") then
					if RayCast(closestPlayer, 0.1) then
						path:Stop()
						Humanoid:MoveTo(closestPlayer.Character.HumanoidRootPart.Position)
					else
						path:Run(closestPlayer.Character.PrimaryPart.Position)
						path.Blocked:Connect(function()
							if closestPlayer and closestPlayer.Character then
								SimplePath:Run(closestPlayer.Character.PrimaryPart)
							end
						end)

						path.WaypointReached:Connect(function()
							if closestPlayer and closestPlayer.Character  then
								path:Run(closestPlayer.Character.PrimaryPart.Position)
							end
						end)
					end
				end
			end
		end
	end
end)

(I’m using the Original SimplePath, I mean I didn’t make any changes)

I would love for someone to help me with this situation. I would appreciate any help. Thanks.

1 Like

Animations are usually the culprit. Try disabling the animations completely and see if the stutter still happens or not. I used to have an issue like this but it was a bug and Roblox fixed it.