Let's take an example. M can be Aluminum $Al$. In order for $Al$ to have an outer electronic layer equal to 0 or 8, it can either loose 3 electrons and become an ion similar to Neon. But it can also use its 3 electrons to make covalent bonds with three $Cl$ atoms, and still add a 4th $Cl^-$ ion, which brings one doublet. In this option, the Aluminum is surrounded by 3+3+2 = 8 electrons, and it looks like Argon. This is the most favorable choice, because it would cost much more energy to remove three electrons. Three !
It is quite different for metals having 2 electrons in their outer shell, like $M$g. If $Mg$ wants to look like a noble gas, it may loose 2 electrons, and become $Mg^{2+}$ which is similar to $Ne$. Of course it costs energy to remove 2 electrons, but not that much. And it would be much more difficult to make covalent bonds. With its 2 electrons, $Mg$ could make two covalent bonds with $Cl$ atoms. But 4 electrons are still missing to look like Argon. These 4 electrons may be brought by 2 $Cl^-$ ions, making $MgCl_4^{2-}$. But this structure is not favorized, because the two approaching $Cl^-$ ions repell one another.
All that to explain why, combined with Chlorine, $Al$ does not make $Al^{3+}$ and prefers covalent structures like $AlCl_4^-$ions, and why $Mg$ prefers to make $Mg^{2+}$ ions