This post originally appeared in Business Insider.

A long time ago on a film set far, far away, two directors made a friendly bet about a small space-fantasy film called Star Wars.

Steven Spielberg would go on to win the bet and take millions of dollars from George Lucas over time. Here’s how the out-of-this-world wager came to be.

The Troubles of The Star Wars

In the mid-1970s, science-fiction films weren’t very popular.

1968’s 2001: A Space Odyssey led the way for the genre but made just $56 million at the box office. So when a young director named George Lucas was trying to sell a script called The Star Wars, based on Flash Gordon space serials, not many studios were eager to make it.

It wasn’t until Lucas took his script to 20th Century Fox that the film finally received backing—but even then it was more because of the success of Lucas’ previous film, American Graffiti.

The challenges didn’t stop there. A delayed, over-budget production caused the troubled director to visit a friend shooting his own sci-fi film in Mobile, Ala.

That friend was Steven Spielberg and the film was his 1977 classic, Close Encounters of the Third Kind.

The Bet

According to an interview with Spielberg for Turner Classics Movies, Lucas came to the set of Close Encounters out of sorts after problems with his passion project. Needing to recharge, Lucas spent a couple of days on set.

“George came back from Star Wars a nervous wreck,” Spielberg said. “He didn’t feel Star Wars came up to the vision he initially had. He felt he had just made this little kids’ movie.”

After a few days, Lucas realized something else: Spielberg’s Close Encounters was going to be so much more successful than Star Wars. So much more that he felt like making a bet with Spielberg.

Spielberg would later say:

He said, “Oh my God, your movie is going to be so much more successful than Star Wars! This is gonna be the biggest hit of all time. I can’t believe this set. I can’t believe what you’re getting, and oh my goodness.” He said, “All right, I’ll tell you what. I’ll trade some points with you. You want to trade some points? I’ll give you 2.5 percent of Star Wars if you give me 2.5 percent of Close Encounters.” So I said, “Sure, I’ll gamble with that. Great.”

Close Encounters would indeed be a hit, making $303 million at the box office. However, Star Wars would go on to become one of the biggest box-office hits of all time.

A Financial Empire

Spielberg’s gamble paid off. Big time.

Released May 25, 1977, on a budget of $11 million, Star Wars—later retitled Star Wars, Episode IV: A New Hope—went on to make $460 million in the U.S. alone.

Adjusted for inflation, the film has made $1.48 billion at the box office, making it the second-biggest box-office hit of all time—only behind Gone With The Wind. By our calculations, this means Spielberg’s 2.5 percent could have made him nearly $40 million.

“Close Encounters was just a meager success story. Star Wars was a phenomenon,” Spielberg said a few years ago. “Of course I was the happy beneficiary of a couple of net points from that movie, which I am still seeing money on today.”

Sequels The Empire Strikes Back and Return of the Jedi would also go on to be great successes leading to prequels, rereleases, and merchandising, making the Star Wars franchise one of the biggest in Hollywood, reportedly worth $30 billion and growing.

See also: New Star Wars Sequel Will Take Place 30 Years After Return of the Jedi