The problem is that their efficiency at converting electricity into projectile kinetic energy is really bad (like single digit percentages bad), getting electrical energy and power density is quite difficult in the first place (capacitors have abysmally bad energy density compared to gunpowder), and coils absolutely hate having their current changed quickly (which you need for this to work).
The problem with coil guns in particular is the ferrous slug is drawn to the center of the magnetic field. The field has to be collapsed at the right time to avoid sapping velocity from the slug, counterproductively.
Many designs that achieve respectable velocities use a multi-stage coil, which requires precise timing for each magnetic field, a lot of power, and high current capability. Generally, that means large batteries for a power source and large capacitors to feed the coils, which becomes heavy and expensive.
Even rifle variants rarely make more energy than a .22 LR, a feat which is easily overshadowed by air guns several hundred years old.
The electromagnet isn't the issue, it's the capacitors to power the magnets. A coil gun capable of matching a small handgun would be too heavy to reasonably carry. At the scale where they could become competitive with a conventional gun, you have an artillery piece.