I just made a script with the code you posted, and ran it on a guy carrying an m4, and it ran fine - no errors, he had an m16/m203 with one m16 mag when it was done.
What exactly happens on your end when you run it? Does he have the magazine in his inventory or not? Or is the original ammo not getting removed, what?