A would-be robber in Pennsylvania had poor timing when he pulled a gun on his taxi driver with a sheriff's deputy behind him, police say.

A Berks County sheriff's deputy approached the cab after it failed to move through a green light Monday afternoon in Reading.

Surveillance video from inside the cab shows the deputy's cruiser pulling up behind the Reading Metro taxi on a city street. Shortly after that, a passenger in the back seat wearing a white hoodie and a mask covering his face aims a gun at the driver through the partition.

"Give me all your money, man," he yells, pointing the weapon, which turned out to be a pellet gun, at the driver's head. "I need it all right now if you don't want to die."

As the panicked driver tells him he has no money, he notices the deputy approaching the cab.

"There's a cop behind us," the driver says.

The robber apparently tries to get out of the taxi and run as Deputy Terry Ely asks if there's a problem. Seeing the gun, Ely draws his firearm and orders the suspect out of the taxi then calls for backup.

The cab driver can be heard telling Ely, "You're a lifesaver."

Eighteen-year-old Victor Martinez-Herrera was arrested. He remained jailed Tuesday on charges including robbery and terroristic threats. A lawyer wasn't listed for him in online court documents.

A call to the Berks County sheriff's department wasn't immediately returned.