Pathfinding NPC stuttering

  1. What do you want to achieve?
    Smooth pathfinding of my NPC.

  2. What is the issue?
    NPC starts stuttering whenever root part anchores or when I get close to it, “SetNetworkOwner” function will not work because it can’t set the network of anchored parts or parts welded to ancored parts.

  3. What solutions have you tried so far?
    I have tried to use “Magnitude” but it didn’t worked, it was still stuttering a bit.

Video about “Server VS Client scrpt” also didn’t helped, so I guess DevForum is my only solution.

local PathfindingService = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")

-- NPC and Humanoid
local NPC = script.Parent -- Assuming the script is a child of the NPC
local humanoid = NPC:FindFirstChildOfClass("Humanoid")
local root_part = NPC:WaitForChild("HumanoidRootPart")
local not_allowed = "Enemy" or "Helper"
local distance
local Cooldown = false


-- Get all waypoints tagged with "Waypoint"
local waypoints = CollectionService:GetTagged("Waypoint")

-- Function to move NPC to a target
local function moveTo(targetPosition)
	local path = PathfindingService:CreatePath({
		AgentRadius = 2,
		AgentHeight = 5,
		AgentCanJump = true,
		AgentJumpHeight = 10,
		AgentMaxSlope = 45
	})

	path:ComputeAsync(root_part.Position, targetPosition)
	local pathWaypoints = path:GetWaypoints()

	if path.Status == Enum.PathStatus.Success then
		for _, waypoint in ipairs(pathWaypoints) do
			
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait(0.25)
			--[[repeat 
				distance = (waypoint.Position - root_part.Position).magnitude
				wait()
			until
			distance <= 4]]
		end
	else
		warn("Path computation failed")
	end
end

-- Function to select a random waypoint
local function getRandomWaypoint()
	if #waypoints == 0 then
		warn("No waypoints found! Ensure parts are tagged with 'Waypoint'.")
		return nil
	end
	return waypoints[math.random(1, #waypoints)]
end

-- Patrol Loop
local function patrol()
	while true do
		local nextWaypoint = getRandomWaypoint()
		if nextWaypoint then
			moveTo(nextWaypoint.Position)
		end
	end
end

-- -- -- Jumpscare
root_part.Touched:Connect(function(Part)
	local Player = game.Players:GetPlayerFromCharacter(Part.Parent)
	local PlayerHumanoid = Part.Parent:FindFirstChild("Humanoid")
	if Player and PlayerHumanoid and not Part.Parent:FindFirstChild(not_allowed) and Cooldown == false then
		if PlayerHumanoid.Health ~= 0 then
			NPC.State.Value = "Killing"
			Cooldown = true
			root_part.Anchored = true
			PlayerHumanoid.WalkSpeed = 0
			wait(0.7)
			PlayerHumanoid.Health = 0
			wait(2)
			Cooldown = false
			NPC.State.Value = "Patrolling"
			root_part.Anchored = false
		end
	end
end)

patrol()

I hope someone will be able to help me, I’m struggling for a year now!

2 Likes

Anchored to what?

There shouldn’t be anything obstructing the NPC while their moving or doing anything.

1 Like

There is says “HumanoidRootPart.Anchored = true” or “PrimaryPart.Anchored = true”.

1 Like

For example, in Piggy, bots/skins anchored their root part and not speed, because root part freezes and makes the jumpscare stable, unlike wakspeed, where bot can still be pushed by psychics.

1 Like

I also just realized that it will start stuttering when I get close to it without dying.

You should try WaypointSpacing

How does it work thought? It wasn’t working before.

Whoops, setting it to 0.2 was a big mistake. But it still stutters, regardless of it being big or small.

You should try making it follow waypoint[2], it may work

I’ll try it tomorrow, but if it doesn’t, is there anything else that I could possibly do?

If it doesn’t work, make it move to the player instead of making a path, after checking if the player is visible in case of an obstacle and close enough distance to the NPC

This is just a patrol script, going part to part, nothing else.

My mistake, then ignore my last reply

No problem, so about Waypoint[2], do I make it like this:

return waypoints[math.random(2, #waypoints)]

Try this:

humanoid:MoveTo(waypoint[2].Position)

Is it only client sided, or does it also happen server side?

The script is made like a server script in npc.

What would that function change btw?

Sorry, I was using my phone, so I wasn’t able to try the script. It could be because it’s on a loop, changing the target position.

I’ll try it tomorrow, if it works, I’ll mark it as a solution.

2 is not a valid membe of ‘PathWaypoint’

that’s all it says.