Matt Damon started writing "Good Will Hunting" (1997) as a final assignment for a playwriting class he was taking at Harvard University. Instead of writing a one-act play, Damon submitted a 40-page script. He wrote his then-girlfriend, medical student Skylar Satenstein (credited in the closing credits of the film), into his script (Satenstein later married Metallica drummer Lars Ulrich). Damon said the only scene from that script that survived — "it survived verbatim" — was when Will Hunting (Damon) meets his therapist, Sean Maguire (Robin Williams) He then came to Ben Affleck and asked him to develop the screenplay together, and the two completed the script in 1994. At first, it was written as a thriller about a young man in the rough-and-tumble streets of South Boston who possesses a superior intelligence and is targeted by the government with heavy-handed recruitment.
Castle Rock Entertainment bought the script for $675,000 against $775,000, meaning that Damon and Affleck would stand to earn an additional $100,000 if the film was produced and they retained sole writing credit. Castle Rock president Rob Reiner urged them to drop the thriller aspect of the story and to focus on the relationship between Will and his therapist. Terrence Malick told Affleck and Damon over dinner that the film ought to end with Will's decision to follow his girlfriend Skylar to California, not them leaving together.
At Reiner's request, screenwriter William Goldman read the script. Goldman consistently denied the persistent rumor that he wrote Good Will Hunting or acted as a script doctor. In his book "Which Lie Did I Tell?" Goldman jokingly writes, "I did not just doctor it. I wrote the whole thing from scratch," before dismissing the rumor as false and saying his only advice was agreeing with Reiner's suggestion.
Affleck and Damon proposed to act in the lead roles, but many studio executives said they wanted Brad Pitt and Leonardo DiCaprio. Meanwhile, Kevin Smith was working with Affleck on Mallrats and with both Damon and Affleck on "Chasing Amy" (1997). Castle Rock put the script in turnaround, and gave Damon and Affleck 30 days to find another buyer for the script who would reimburse Castle Rock the money paid, otherwise the script reverted to the studio, and Damon and Affleck would be out. All the studios that were involved in the original bidding war for the screenplay now turned the pair down, taking meetings with Affleck and Damon only to tell them this to their face. As a last resort, Affleck passed the script to his "Chasing Amy" director Kevin Smith, who read it and promised to walk the script directly into Harvey Weinstein's office at Miramax. Weinstein read the script, loved it, and paid Castle Rock their due, while also agreeing to let Damon and Affleck star in the film. In his recollection of the meeting, Weinstein asked about an out-of-place, mid-script oral sex scene, which Damon and Affleck explained was a test to see which studio executives had actually read the script.
The very first day of the shooting, Matt Damon and Ben Affleck started crying out of happiness, because it was a scene between Robin Williams and Stellan Skarsgård, accomplished actors, doing Damon's and Affleck's scene verbatim, and they had waited so long (five years) for this to happen.