A Kilwinning accounts manager pulled a gun on an ice cream seller as he tried to rob him of his takings - then sent him a Facebook message saying sorry.
And the Facebook message Ian Hamilton, 30, sent proved to be his undoing - as he admitted he was responsible and revealed his identity.
Hamilton, who is understood to make around £26,000 in his job with TSB, approached the ice cream van as it was parked in the Pennyburn area of the town at 3.30pm on July 3 last year.
Hamilton asked for "20 fags" while holding his hand behind his back, then produced the weapon and said: "You're getting robbed."
Despite having the gun pointed in his face, the seller remained calm.
He grabbed hold of the weapon and tried to wrestle it from Hamilton's grasp while the panicked would-be robber repeatedly shouted: "Let go!"
Hamilton managed to keep hold of the gun and then made off, while the ice cream seller drove away from the scene of the attack and called police.
An investigation was launched but it was not clear who was responsible as the man recognised Hamilton as someone local to the area but did not know him personally or know his name.
Hamilton was found asleep face-down on top of his bed, with the gun next to him, later that day by his girlfriend.
Never having seen the weapon before, she put it in the kitchen cupboard.
He had stolen the weapon, which was a ball-bearing gun, from his friend that morning during a drinking session.
But a sheepish Hamilton contacted the ice cream seller via Facebook the following morning to apologise - revealing his identity and admitting what he'd done.
His message, read: "Alright bud. Just wanted to say soz for pointing that gun at you, it was only a toy.
"It's a bit daft, I know, but I had a bevvy in me and I thought it was funny.
"I know it's not, though.
"I've got rid of it now so it doesn't cause any problems."
The ice cream seller was able to inform police probing the attempted robbery of the message and Hamilton was arrested over the offence.
The details emerged on Tuesday when Hamilton appeared in the dock at Dumbarton Sheriff Court to admit his guilt over the offence.
The case was originally prosecuted at Kilmarnock Sheriff Court but was transferred to Dumbarton to be heard.
Procurator Fiscal Depute David McDonald explained: "Mr Hamilton asked for, '20 fags' and did not specify a brand.
"Mr Hamilton kept moving his hand behind his back.
"As he turned back to face Mr Hamilton he heard Mr Hamilton say, 'You're getting robbed'.
"He placed the cigarettes on the counter and said, 'What?'
"At this Mr Hamilton brought his right hand from behind his back and pointed what appeared to be a handgun in to the van, at Mr McIntyre.
"Mr Hamilton appeared to cock the weapon.
"As he went to grab the cigarettes Mr Hamilton has pointed the gun further in to the van.
"He grabbed the gun and tried to take it from Mr Hamilton.
"There was a short struggle.
"Mr Hamilton was repeatedly shouting 'let go'.
"He did let go and the struggle was over in a matter of seconds."
During Tuesday's hearing, Hamilton struck a deal with prosecutors to plead guilty to one charge in exchange for another being dropped.
He admitted assaulting the man with intent to rob him by "present[ing] a firearm at him, stat[ing] he was being robbed and struggl[ing] with him."
Defence solicitor Fraser Alexander reserved his plea in mitigation until the next occasion and Sheriff Simon Pender called for background reports ahead of sentencing.
Hamilton, of Dalgarrock, Pennyburn, Kilwinning, could be jailed for up to five years for the offence when he returns to the dock next month to learn his fate.