How to avoid npc stuttering?

So I made an npc pathfinding script for a horror game and it works fine until it starts to pathfinding, when it pathfinds it goes to a waypoint, stops for a tiny bit, and walks to the next. Any way to fix this?

local agentParams = {
	["AgentHeight"] = 5,
	["AgentRadius"] = 2,
	["AgentCanJump"] = true,
	Costs = {
		Doors = 1
	}
}
local path = pathfindingService:CreatePath(agentParams)
path:ComputeAsync(humanoidRootPart.Position, target.Position)
local waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
	for _, waypoint in ipairs(waypoints) do
		if waypoint.Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
		humanoid:MoveTo(waypoint.Position)
		if checkSight(target) then
			repeat
				wait()
				humanoid:MoveTo(target.Position)
				if target == nil or target.Parent.Humanoid == nil or target.Parent.Humanoid.Health < 1 or humanoid.Health < 1 then
					break
				end
			until not checkSight(target)
			break
		end
		humanoid.MoveToFinished:Wait()
	end
else
	repeat
		wait()
		local xRand = math.random(-5, 5)
		local zRand = math.random(-5, 5)
		local goal = humanoidRootPart.Position + Vector3.new(xRand, 0 ,zRand)
		humanoid:MoveTo(goal)
		humanoid.MoveToFinished:Wait()
		break
	until path.Status == Enum.PathStatus.Success
end
1 Like

Remove the wait() in the repeats. Or replace them with task.wait().

2 Likes

The npc is still stuttering when pathfinding :confused:

Remove this line. Also don’t even put task.wait() now and see if it works

When I removed the task wait it would say “exhausted allowed execution time” in output and when i removed movetofinished it wont pathfind at all

What is this function checkSight() ?

It checks when the target is in a clear line of sight and if they are then it moves to their position

What is the point of that though? I don’t think its needed

If I remove it then the npc will move to where the target previously was instead of target position

Are you trying to make a system where the npc chases a player?

yes and the player is the target

Is there anything else I could try?

Waits are actually SUPER important and can be the difference between a 0.015-second delay to ensure the system doesn’t encounter an exhaust, and the system failing because of an exhaust. It is actually more so encouraged to use them, and they ONLY wait the said amount of time, no more or less. However, in the instance of this script, it would be better to use task.delay.

I recommend using task.delay(humanoid:MoveTo(), 0.01) in this instance, instead of task.wait! (task.wait is always good, however, task.delay does the same thing, just you can control it better!)

The pathfinding issue is still not fixed

Not to disappoint you, but I haven’t figured this out as well. I would instead use something else than the pathfinding service ROBLOX gives you, as its really bad. You can either raycast the npc to its target or use a custom pathfinding module to help you. (Legacy) SimplePath - Pathfinding Module - #33 by V3N0M_Z

delay isn’t really encouraged because it stacks up as the game runs longer.

Sorry for not replying. I actually had a solution then but my computer just started glitching real bad

local path = pathfindingService:CreatePath(agentParams)
path:ComputeAsync(humanoidRootPart.Position, target.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
    waypoints = path:GetWaypoints()
	if waypoint.Action == Enum.PathWaypointAction.Jump then
		humanoid.Jump = true
	end
	humanoid:MoveTo(waypoint.Position)
	humanoid.MoveToFinished:Wait()
end

This could work though I’m not very sure

Without the checksight function the AI is really bad, I don’t think that’s the problem

Have you at all thought of the NPC’s network owner? You should set it to nil so no client can manipulate it.