HINGHAM — Elementary school students got a lesson in determination as part of a special program aimed at getting kids to set goals and to get active.

“The worst injustice you can ever do for yourself is to underestimate your own ability,” Dave McGillivray, an author, philanthropist and road race director, told the student body of Plymouth River School on Friday morning.

McGillivray was there to promote the “Dream Big Marathon,” a program that encourages students to complete 26 sessions of exercise, perform 26 acts of kindness and read 26 books as a way to practice setting and achieving ambitious goals.

Over two weeks, he visited East, South and Foster elementary schools, along with Plymouth River. Students at all four schools are participating in the Dream Big program. This week, students read “Dream Big,” a 2018 children’s book by McGillivray that chronicles his experience trying and failing to run the Boston Marathon, then coming back to run the race every year since.

During an assembly, McGillivray told the Plymouth River students about his own setbacks and accomplishments, mixing in lessons he’s learned along the way.

A Medford native, McGillivray dreamed of one day playing for the Red Sox or Celtics. But his short stature quickly made it obvious these dreams would never come to fruition. He spent his childhood getting picked last for teams during every schoolyard ballgame. The sting of that disappointment stuck with him, he said.

“That was a really tough thing to overcome, because I got this feeling of not being needed, not being wanted,” he said.

Eventually, he decided to pick up running, a sport that meant he would never get picked last or get cut from a team.

Decades later, he estimates he has run 150,000 miles since then, much of it to raise money for various charities. He ran across the country, averaging 43 miles a day for 80 days, to raise money for the Jimmy Fund, a Boston charity that funds cancer research and treatment. He’s run seven marathons in seven days on seven continents, and completed eight Ironman triathlons, which combine a marathon with a 2.4-mile swim and a 112-mile bicycle ride.

The first time he attempted the Boston Marathon, at 17, he had to stop around mile 20 and was taken to a hospital in an ambulance, he told the students. He vowed to finish it the next year, and has not missed it once in the 47 years since.

Thirty-two years ago, McGillivray became the iconic marathon’s race director, which means he cannot run the race during the day, with all the other runners. Now, he runs the course every year after everyone else has finished, after the sun has fallen and the crowds have disappeared.

“My game, my rules,” he said.

All of these experiences, he told the students, have shown the value of not letting potential failures stop them from working toward their goals. Every setback or failure, like dropping out of a race or not making a team, is merely a chance to learn for next time, he said.

Underestimating themselves was the biggest mistake the students could make, he said.

McGillivray explained that he launched the Dream Big Marathon to encourage students to get active and apply the lessons from his book to their lives.

“The only way you fail is not to try,” he said.