Pathfinding doing glitchy moves on a rig

For some reason the moving is very jittery, i don’t know what I did wrong I was following a tutorial, unless it’s something obvious and I can’t see it.

Here’s my code:

function followPath(destination, model)
	destination.Parent = workspace
	
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(model.PrimaryPart.Position, destination.Position)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		
	local CurrentWaypointIndex = 2
	local MovedToFinishConnection:RBXScriptConnection
	local PathBlockedConnection:RBXScriptConnection
	
	waypoints = path:GetWaypoints()
	
	local MovedToFinishConnection:RBXScriptConnection

		model.PrimaryPart:SetNetworkOwner(nil)
		
		if not walkAnim.IsPlaying then
			walkAnim:Play()
		end
		
		MovedToFinishConnection = model.Humanoid.MoveToFinished:Connect(function(reached)
			if reached then
				if CurrentWaypointIndex < #waypoints then
					CurrentWaypointIndex += 1
					model.Humanoid:MoveTo(waypoints[CurrentWaypointIndex].Position)
				else
					MovedToFinishConnection:Disconnect()
					destination.Parent = area.AiParts
					walkAnim:Stop()
				end
			else
				MovedToFinishConnection:Disconnect()
				followPath(destination, model)
			end
		end)
		
		PathBlockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= CurrentWaypointIndex then
				MovedToFinishConnection:Disconnect()
				PathBlockedConnection:Disconnect()
				
				model.Humanoid.WalkToPoint = model.PrimaryPart.Position
				followPath(destination, model)
			end
		end)
		
		model.Humanoid:MoveTo(waypoints[CurrentWaypointIndex].Position)
	
	else
		warn("Path not computed!", errorMessage)
	end
end

Any help is appreciated

I have experienced this problem before, it’s probably just that your function ran more than one time and it created twice more waypoints than usual, then the worm tries to reach all of them.

Try adding a debounce variable that you can only fire the function with once it’s false. Once the function runs, set it to true. Then once everything in the function is done, set it to false.

1 Like

Yeah it was firing multiple times and i fixed it, but the issue still persists

Updated code
local waypoints
local CurrentWaypointIndex
local numOfWaypoints
local MovedToFinishConnection:RBXScriptConnection
local PathBlockedConnection:RBXScriptConnection

function followPath(destination, model)
	print("a")
	destination.Parent = workspace
	
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(model.PrimaryPart.Position, destination.Position)
	end)

	local tweenPart

	if success and path.Status == Enum.PathStatus.Success then
	
		waypoints = path:GetWaypoints()
		numOfWaypoints = #waypoints
		
		local MovedToFinishConnection:RBXScriptConnection
		
		model.PrimaryPart:SetNetworkOwner(nil)
		
		if not walkAnim.IsPlaying then
			walkAnim:Play()
		end
		
		PathBlockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= CurrentWaypointIndex then
				PathBlockedConnection:Disconnect()
				followPath(destination, model)
			end
		end)
		
		if not MovedToFinishConnection then
			MovedToFinishConnection = model.Humanoid.MoveToFinished:Connect(function(reached)
				if reached and CurrentWaypointIndex < #waypoints then
					CurrentWaypointIndex += 1
					model.Humanoid:MoveTo(waypoints[CurrentWaypointIndex].Position)
				else
					PathBlockedConnection:Disconnect()
					MovedToFinishConnection:Disconnect()
					
					destination.Parent = area.AiParts
					walkAnim:Stop()
				end
			end)
		end
		
		CurrentWaypointIndex = 2
		model.Humanoid:MoveTo(waypoints[CurrentWaypointIndex].Position)
	
	else
		warn("Path not computed!", errorMessage)
	end
end

I’ve noticed that after reaching the end point for the first time and going to pathfind again that’s when it starts glitching out and rotating weird