A 29-year-old North Carolina man who fired a military-style assault rifle inside a popular Washington pizzeria in December, wrongly believing he was saving children trapped in a sex-slave ring, was sentenced on Thursday to four years in prison.

The man, Edgar Maddison Welch, drove on Dec. 4 from his hometown, Salisbury, N.C., to the Comet Ping Pong restaurant with three guns. He was investigating unfounded but widespread online reports of children held there in a child abuse scheme led by Hillary Clinton, a theory known as “Pizzagate.” But Mr. Welch, who pleaded guilty to federal weapons charges in March, rescued no children. Rather, he frightened employees and patrons, who panicked and ran.

Mr. Welch surrendered after the episode and almost immediately apologized, saying he had made an “incredibly ill-advised decision” to try to save endangered children who were never there. “The intel on this wasn’t 100 percent,” Mr. Welch, who goes by his middle name, Maddison, said in an interview with The New York Times after his arrest.

The judge, Ketanji Brown Jackson of Federal District Court in Washington, sentenced Mr. Welch to four years in prison and three years of probation. He must pay $5,744 in restitution for property damage at the pizzeria.