ROBIN WILLIAMS was mired in money problems before his death with his charity faltering and the cost of two messy divorces taking a painful financial toll. 

But The National ENQUIRER has learned his children can take comfort that their devoted dad took steps to secure their future.

After the birth of his first son, the comedy genius established a trust that would cover every need. Years later, he opened another trust covering his other children as well. But the fate of Robin’s The Windfall Foundation isn’t as bright. The charity went from donating $576,947 in 2007 to nothing in 2011, and had just $2,276 in the bank in 2012, according to documents examined by The ENQUIRER.

His divorces from Valerie Velardi and Marsha Garces cost the entertainer a whopping $30 million, and a close source disclosed that Robin was experiencing “serious money troubles.”

In September 2013, Robin said: “Divorce is expensive. I used to joke they were going to call it ‘all the money,’ but they changed it to ‘alimony.’ It’s ripping your heart out through your wallet.”

He noted, “There are bills to pay. My life has downsized, in a good way.”

In need of cash, Robin put his beloved, 640-acre Napa Valley estate up for sale. The nine bedrooms, six bathrooms Portuguese-style home was first listed in 2012 for $35 million, but was officially put up for sale in April this year for $29.9 million and is still on the market.

But his children remained shielded from his financial reversals.

Back in Dec. 1989, Robin established a financial trust, roughly six years after the birth of his son, Zachary “Zak” Pym 
Williams, with first wife Valerie, and just months after the arrival of his daughter Zelda, born on July 31, 1989 to his second wife, Marsha.

By March, 2011, Valerie was removed from the trust because “all spousal support obligations” had been satisfied, and Zak was named its “sole beneficiary,” with funds earmarked for support, education and medical care. In addition, the generous dad had established another trust covering all the children in Dec. 2009.

Farsighted Robin set it up to disburse the funds in stages – one third of the principal at age 21, one half of the remaining principal at 25, and the rest at 30.