NumberOfMagazines = {_x == "GlockMag"} count (Magazines player)
Note that this only counts the number of clips carried, not how much ammunition is in each. If you swap them one-for-one, it's possible for the player to game the system by running the script to replenish every mag. Most people in the past have removed one clip from the player each time, by assuming that at least one mag is likely to be mostly empty. Because this was generally done for primary weapons, it seems fair. For handguns, when you can carry at most 4 clips, this might be a bit harsh.