AI freezes randomly when pathfinding, movetofinished returns false

Simply put, I spawn an AI and assign it a position, I print the waypoint positions yet for some reason the AI struggles to get to them, the moveto times out despite the AI being entirely able to get there. I am stumped at this point and have no idea what the issue could be, I thought it was the nav mesh but the nav mesh seems fine.

Here’s an image showing the path being computed, the AI is stuck where he stands though.

function AIService:PathfindToPosition(agent, endPosition)
	
	if agent.activeTask then return end
	
	print("Running pathfind")
	
	local selected_agent = agent
	local start_position = selected_agent.npc.PrimaryPart.Position
	local agent_humanoid = selected_agent.npc:FindFirstChild("Humanoid")
	local end_position = endPosition
	
	agent.activeTask = true
	
	local path = PathfindingService:CreatePath({
		AgentCanJump = true,
		AgentHeight = 5,
		AgentRadius = 3,
		Costs = {
		}
	})
	
	local success, error_message = xpcall(function()
		path:ComputeAsync(start_position, end_position + Vector3.new(8, 0, 0))
	end, function()
		warn("Path not computed")
		return false
	end)
	
	local path_waypoints
	local reached_connection
	local blocked_connection
	local next_index
	
	if success and path.Status == Enum.PathStatus.Success then
		
		path_waypoints = path:GetWaypoints()
		
		for _, waypoint in ipairs (path_waypoints) do
			local part = Instance.new("Part")
			part.Size = Vector3.new(1,1,1)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
			part.Position = waypoint.Position
			part.Material = Enum.Material.Neon
		end
		
		blocked_connection = path.Blocked:Connect(function(blocked_index)
			if blocked_index >= next_index then
				blocked_connection:Disconnect()
				agent.activeTask = false
			end
		end)
		
		if not reached_connection then
			reached_connection = agent_humanoid.MoveToFinished:Connect(function(reached_bool)
				print(reached_bool)
				if reached_bool and next_index < #path_waypoints then
					next_index += 1
					agent_humanoid:MoveTo(path_waypoints[next_index].Position)
				else
					reached_connection:Disconnect()
					blocked_connection:Disconnect()
					agent.activeTask = false
				end
			end)
		end
		
		next_index = 2
		agent_humanoid:MoveTo(path_waypoints[next_index].Position)
		
	end

end

Have you tried setting the NPC’s NetworkOwnership to nil?
Last time i got stuck with NPC’s that helped. :thinking:

local NPC = script.Parent --Set NPC to your NPC
For _,part in pairs(NPC:GetChildren()) do
if part:IsA("BasePart") then
part:SetNetworkOwner(nil)
end
end
2 Likes

Thank you very much, that helped me alot in my case.

1 Like