President Trump and Gov. Rick Scott of Florida, a Republican, both declined invitations to participate in the town hall-style meeting.
The father of a 14-year-old girl who was killed at Stoneman Douglas angrily lectured Mr. Rubio for his refusal to support gun control legislation.
The father, Fred Guttenberg, demanded that Mr. Rubio explain his opposition to a ban on the kind of assault weapon that the gunman at the school used to shoot his daughter, Jaime.
“My daughter, running down the hallway, was shot in the back with an assault weapon, the weapon of choice,” Mr. Guttenberg said during the forum as Mr. Rubio stood stone-faced. “It is too easy to get. It is a weapon of war. The fact that you can’t stand with everybody else in this room and say that, I’m sorry.”
The room erupted in applause for Mr. Guttenberg and repeatedly booed as Mr. Rubio sought to explain his opposition to a proposed assault weapons ban. The bill, he said, is riddled with loopholes that would make it easy for criminals to get around.
“First, you have to define what it is. It basically bans 220 specific models of gun,” Mr. Rubio said, prompting applause from the audience.
He continued, saying that the bill also allows for “2,000 other types” of guns that operate the same way but are not classified as assault weapons.
Ms. Loesch repeatedly deflected questions about restrictions on the availability of guns, insisting instead that keeping guns out of the hands of the mentally ill or people with criminal records would keep students safe.
“I don’t believe that this insane monster should have ever been able to obtain a firearm, ever,” Ms. Loesch told Emma González, a senior at the high school. “None of us support people who are crazy, who are a danger to themselves, who are a danger to others, getting their hands on a firearm.”
But her answers prompted angry taunts from the audience. At times, Ms. Loesch struggled to answer, but she steadfastly refused to back down on the N.R.A.’s position opposing new gun restrictions.
She said the group opposed raising the legal age for purchasing an assault weapon to 21 from 18, an idea that Mr. Trump had earlier signaled support for. Students who are old enough to serve in the military should be allowed to own firearms, she said.
Diane Wolk Rogers, a history teacher at Stoneman Douglas, confronted Ms. Loesch by asking her to defend the idea that allowing the suspect in the shooting, a 19-year-old, to own an assault rifle represents “a well-regulated militia” as is written in the Second Amendment.
Ms. Loesch said the phrase was meant to protect the rights of anyone who “could operate and service their firearm,” an answer that drew loud yelling and more boos from the audience.
Mr. Rubio, for his part, had offered opening remarks that he clearly hoped would deflect criticism during the event. He expressed grief for those who were affected by the shooting, but he said that people in the United States needed to find ways to disagree “without accusing one another of being evil people.”
But the audience, including Mr. Guttenberg, did not hold back in criticizing Mr. Rubio’s position on gun control. Mr. Guttenberg repeatedly asked whether Mr. Rubio believed that “guns were the factor in the hunting of our kids.”
The senator said he agreed, but he insisted that other types of laws would better prevent such attacks. He said that he would support a bill to increase the minimum age for the purchase of an assault weapon to 21 from 18. And he said that he backed improvements to background checks and a ban on so-called bump stocks, which can convert semiautomatic weapons to fire automatically.
Students and teachers at the forum on Wednesday also had questions for Florida’s other politicians, including two Democrats, Senator Bill Nelson and Representative Ted Deutch.
One young student talked about how her best friend was killed in front of her, and she asked Mr. Nelson about background checks, and how to improve them.
Another student, whose brother died in the shooting, bluntly said to Mr. Deutch that “my friends and I are worried that we are going to be murdered in our classrooms.” He asked what Mr. Deutch was going to do about it.
“As a starter,” the congressman said, “I’m going to introduce legislation to make sure that assault weapons are illegal in every part of this country.”
The audience burst into applause.
But Mr. Rubio, who ran for president in 2016, was repeatedly challenged on his positions on gun control and his willingness to accept donations from the N.R.A.
The issue of donations was raised by a junior at Stoneman Douglas who stood just feet from the senator and asked him whether he would renounce money from the gun advocacy group in the future. When Mr. Rubio refused, the audience booed loudly.
“People buy into my agenda, and I do support the Second Amendment,” Mr. Rubio said, repeatedly refusing to say he would stop accepting money from the group. “The influence of these groups comes not from money, the influence comes from the millions of people who support the agenda.”
Mr. Deutch at one point engaged in a brief debate with Mr. Rubio, saying that a gun allowing someone to fire off 150 rounds in six to seven minutes “should be banned.”
Later, Chris Grady, a student at the high school, thanked Mr. Rubio for appearing at the forum, unlike Mr. Trump and Mr. Scott.
“A lot more than can be said for our so-called president and governor,” Mr. Grady told Mr. Rubio. “We need you and your colleagues on both sides to come together with us and find a compromise if we are ever to solve this epidemic.”
Mr. Grady asked the senator whether he supported legislation to limit high-capacity magazines that allow weapons to fire many bullets quickly. Mr. Rubio said that he had long opposed such legislation, but was now reconsidering that position.
The senator said that there could be evidence from the Florida school shooting that suggested the gunman might have killed fewer people if he did not have high-capacity magazines.
“It wouldn’t have prevented the attack, but it would have made it less lethal,” Mr. Rubio said.