Ian Hamilton pulled out the weapon and pointed it at Neil McIntyre in Kilwinning, Ayrshire, in July 2016.
Hamilton, who is from Kilwinning, said "you're getting robbed" as he tried to force Mr McIntyre, 31, to hand over his takings.
The 30-year-old pled guilty to a charge of assault with intent to rob.
Dumbarton Sheriff Court heard that Hamilton approached Mr McIntyre's ice cream van as it was parked in the Pennyburn area of Kilwinning at about 15:30 on 3 July last year.
He asked for "20 fags" while holding his hand behind his back, then produced the weapon and said: "You're getting robbed."
Mr McIntyre grabbed the ball-bearing gun and tried to take it off him, before Hamilton managed to wrestle the weapon back and escape empty-handed.
The ice cream van owner - who recognised Hamilton as local but did not know his name - reported the attack to the police, who launched an investigation.
Hamilton contacted Mr McIntyre the following morning via his business' Facebook page to apologise.
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 court heard Hamilton had taken the weapon from his friend on the morning of the attack.
Mr McIntyre informed the officers investigating the attempted robbery about the message and Hamilton was arrested.
Defence solicitor Fraser Alexander reserved his plea in mitigation until the next hearing and Sheriff Simon Pender called for background reports ahead of sentencing next month.
A TSB account manager pulled a gun on an ice cream van man as he tried to rob him, then sent him a Facebook message to apologise, a court has heard.