The Enterprise has arrived some days early for a rendezvous with a supply ship, and thus the crew spend the time to pursue personal activities. Data and La Forge propose to Captain Picard to attempt to set up systems that would allow them to use Data's processing abilities to run critical systems in the case of main computer failure, and he allows them to proceed.
Meanwhile, Worf reluctantly joins his son Alexander in a holodeck adventure set in the town of Deadwood, South Dakota in the American Old West, later joined by Deanna Troi. The three play the role of lawmen in Deadwood, where Eli Hollander, the "Butcher of Bozeman", is wanted. Worf tries to use his Starfleet tactics to end the episode quickly, but Alexander insists that he play along with the scenario. They capture Eli, learning that his father Frank is a sly and powerful man.
As Data and LaForge work on the interface, a brief energy surge occurs. Shortly after, several crew find the ships systems to reacting strangely, specifically around elements of Data's logs and records. Similarly, Data starts acting strange, taking up a Western vernacular without his awareness.
In the holodeck, Worf and Alexander meet Frank, who appears exactly like Data. Later, Frank captures Alexander, demanding the release of his son Eli. Worf gets into a gun battle and is wounded, and he and Troi find that the holodeck safety protocols are off and they cannot end the program. They realize Alexander could be in trouble and the only assured way is to play out the story. Further, Troi observes that Eli now also looks like Data and possesses skills comparable to Data's. As more of the holodeck characters take on Data's appearance, Worf works to create a personal shield as protection, knowing he would not be victorious against characters that all have Data's skills.
Data and La Forge determine that the power surge causes segments of the main computer and Data's processes to swap memories, and they start a memory purge to restore both to normal operation.
On the holodeck, Worf and Troi successfully defeat Frank and his gang, and recover Alexander. They believe the story is now over, but the holodeck does not yet respond. Only after Miss Annie, one of the dancers from the local tavern and also now looking like Data, thanks Worf for his bravery and throws herself into Worf's arms, does the story end, allowing the three to leave safely. Data and the ship's computer are restored to normal, and Worf promises to join Alexander on the holodeck for another adventure in Deadwood.