Why do soaps not work in hard water?

Soaps are sodium or potassium salts of long-chain fatty acids. Hard water contains calcium and magnesium ions. When soaps are dissolved in hard water, these ions displace sodium or potassium from their salts and form insoluble calcium or magnesium salts of fatty acids. These insoluble salts separate as scum.
2 \mathrm{C}_{1} \mathrm{H}_{3} \mathrm{COONa}+\mathrm{CaCl}_{2} \longrightarrow 2 \mathrm{NaCl}+\left(\mathrm{C}_{1} \mathrm{H}_{39} \mathrm{COO}\right)_{2} \mathrm{Ca}
calcium steurate
( sовр )
This is the reason why soaps do not work in hard water.