A script doctor is an accomplished screenwriter hired to help a script overcome a few specific, nagging problems, usually just before the movie goes into production.
Script doctors almost never rewrite the entire movie, but instead just work on specific issues that need last-minute addressing.
Script doctors are not credited on films, and therefore do not receive residuals, but they are paid large upfront fees for short — albeit intense and time constrained — periods of work.