Pathfinding module being choppy

Hello fellow devs! So I’ve been working on Pathfinding module for my horror game’s AI, but it seems to be choppy/laggy/jittery and I don’t really know how to fix it, here’s footage :

And there’s the code :

local workspace = game:GetService("Workspace") --because of my anti-exploit

local module = {}
local ps = game:GetService("PathfindingService")

module.__index = module

function module.new(rig : Model,params)
	if not rig then return end
	local self = setmetatable({},module)
	self.rig = rig
	repeat task.wait() until self.rig:FindFirstChildWhichIsA("Humanoid")
	self.hum = self.rig:FindFirstChildWhichIsA("Humanoid")
	self.hrp = self.rig:WaitForChild("HumanoidRootPart")
	self.wps = nil
	self.curWaypoint = 1
	self.lastTime = os.time()
	if params then
		self.Path = ps:CreatePath(params)
	else
		self.Path = ps:CreatePath()
	end
	
	coroutine.wrap(function()
		while task.wait() do
			local diff = os.time() - self.lastTime
			if diff > self.hum.WalkSpeed * 0.8  then
				local sucess,err = pcall(function()
					if self.Path and self.curWaypoint and self.wps then
						if self.curWaypoint < #self.wps then
							self.curWaypoint += 1
						else
							self.curWaypoint = 1
						end
					end
				end)
				if not sucess then
					warn(err)
					self.curWaypoint = 3
				end
				self.lastTime = os.time()
			end
		end
	end)()
	
	return self
end

function module:Run(position : Vector3)
	if self.rig then
		for _,i in pairs(self.rig:GetDescendants()) do
			if i:IsA("BasePart") then
				pcall(function()
					i:SetNetworkOwner(nil)
				end)
			end
		end
	end
	pcall(function()
		if not position then return end
		if not self.Path or not self.hrp then return end
		self.Path:ComputeAsync(self.hrp.Position,position)
		if self.Path.Status == Enum.PathStatus.Success then
			self.wps = self.Path:GetWaypoints()
			if self.wps[self.curWaypoint] then
				if self.wps[self.curWaypoint].Action == Enum.PathWaypointAction.Jump then
					self.hum.Jump = true
				end
				self.hum:MoveTo(self.wps[self.curWaypoint].Position)
			end
		else
			self.hum:MoveTo(position)
		end
	end)
end

return module
1 Like

What’s happening is the zombie or w.e is stopping/starting walking. You can fix this by not waiting till they completely reach the current waypoint to move onto the next waypoint.

Have you even read the code? There’s no checking if Humanoid finished moving.

My mistake. So perhaps the issue is that there are moments where the new path hasn’t been computed yet and the character has reached its position or the moveTo has timed out before the next move is called?

Or I could make it so there’s delay when new path is computed.

You could but the character would still have a walk/stop to it. I assume the goal is a continuous walk.

Yes, that’s the goal, because I don’t want my AI to be easily outrunable.

Maybe you recalculate the path in a separate method and switch to the new path only once its been calculated?

What do you mean by “method”? I don’t really get it.

So I just mean like outside your current algorithm. Start out by calculating the initial path of waypoints to follow. Then set the character on that path. Then have another routine that continues to calculate a new path. When a new path is available, switch the current path to the new path. That way you eliminate any wait time during path calculations. If you still get a bit of glitch step after doing that. Then try removing or skipping the first waypoint in the new path. (Since that point maybe super close to the character)