Pathfinding // Npc breaks randomly

Hi! Im making a guard in my game who wanders around and randomly moves around the areas he/she goes to. I Have been trying to fix it for about 2 hours now and cant find any way to fix it. Here are the issues:

  • NPC sometimes creates jittery motion, but stops after a bit and continues on its way.
  • NPC will break randomly and stop working. Ive tried pcalls and prints to detect the issue but I cant. Happens 5-10 minutes after the npc starts moving.

Here are my scripts.
Main script, inside the character:

local current,pos = nil,0
local last = nil

function Ran(m,l)
	local rng = math.random(m,l)
	if rng == last then
		repeat rng = math.random(m,l) until rng ~= last
	end
	return rng
end

while true do
	local l = nil
	if current == nil then
		current = 'SetPos'
		l = Ran(1,6)
		require(script.AiModule):MoveTo(l)
	else
		if current == 'SetPos' and pos < 4 then
			pos+=math.random(1,2)
			current = 'Wander'
			require(script.AiModule):Wander(32)
		elseif current == 'Wander' then
			current = 'SetPos'
			l = Ran(1,6)
			require(script.AiModule):MoveTo(l)
		end
	end
	if l then
		last = l
	end
	task.wait(math.random(2,8))
	script.Parent.Humanoid:MoveTo(script.Parent.Torso.Position) --Reset it, just incase.
end

Module, inside of the script above:

local module = {}

local Positions = {
	[1] = Vector3.new(-6, 971, -79);
	[2] = Vector3.new(-4, 992, -120);
	[3] = Vector3.new(3, 981, -53);
	[4] = Vector3.new(104, 971, -50);
	[5] = Vector3.new(128, 970, -75);
	[6] = Vector3.new(190, 970, 11);
}

local pFS = game:GetService('PathfindingService')
local h,t = script.Parent.Parent.Humanoid,script.Parent.Parent.Torso

function module:MoveTo(pos)
	local succ, err =  pcall(function()
		local p = pFS:CreatePath()
		p:ComputeAsync(t.Position,Positions[pos])
		local w = p:GetWaypoints()
		for i, way in pairs(w) do
			local wT = 0
			repeat
				if way.Action == Enum.PathWaypointAction.Jump then
					h:ChangeState(Enum.HumanoidStateType.Jumping)
				end
				h:MoveTo(way.Position)
				wT+=1
				if wT > 5 then print(wT) end
				task.wait()
			until (t.Position - way.Position).magnitude < 3 or wT > 50
		end
	end)
	if err then print(err) end
end

function module:Wander(maxDistance)
	local succ, err =  pcall(function()
		local p = pFS:CreatePath()
		p:ComputeAsync(t.Position,t.Position + Vector3.new(math.random(-maxDistance, maxDistance), 0, math.random(-maxDistance, maxDistance)))
		local w = p:GetWaypoints()
		for i, way in pairs(w) do
			local wT = 0
			repeat
				if way.Action == Enum.PathWaypointAction.Jump then
					h:ChangeState(Enum.HumanoidStateType.Jumping)
				end
				h:MoveTo(way.Position)
				wT+=1
				if wT > 5 then print(wT) end
				task.wait()
			until (t.Position - way.Position).magnitude < 3 or wT > 50
		end
	end)
	if err then print(err) end
end


return module

Please help! If you need any extra information please let me know.

Have you tried doing the require once at the top of the main script and the using that as the bases for calling the functions instead of using require for each function call.
I am thinking that using require repeatably maybe causing a memory usage problem.

1 Like