Pathfinding AI having strange movement patterns

I’m not exactly sure why, but whenever my AI finds a target to follow, it starts to tweek out.
https://gyazo.com/cafce68df113c5a1943d1cb58ac20aa3 (near the end)

I’m finding a new path every second, using this function to do so:

function pathToTarget(char, targ)
	local root = char.HumanoidRootPart
	local ehum = targ:FindFirstChild("Humanoid")
	local eroot = ehum and ehum.RootPart
	local varias = char.Variables
	local focus = varias.Focus
	local hum = char.Humanoid
	
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(root.Position, eroot.Position)
	local waypoints = path:GetWaypoints()
	for i,v in pairs(waypoints) do
		if v.Action == Enum.PathWaypointAction.Jump then
			hum.Jump = true
		else
			hum:MoveTo(v.Position)
			sub(function()
				wait(0.5)
				if hum.WalkToPoint.Y > root.Position.Y then
					hum.Jump = true
				end
			end)
			hum.MoveToFinished:Wait()
			if not focus.Value then
				break
			end
			if targcheck(char, focus.Value) then
				break
			end
			if checkdis(eroot,waypoints[#waypoints]) > 20 then
				pathToTarget(char, focus.Value)
				break
			end
			if targ ~= focus.Value then
				break
			end
		end
	end
end

Any idea as to why it does this?

What exactly is this type of function?

1 Like

Basically it, spawns a function outside of the thread, waits half a second, and if where the point the humanoid is going to’s Y axis is above the root’s Y axis, it makes it jump.