Dear student,
Hard water generally contains calcium and magnesium salts and sometimes ferrous salts also.
This is because these are the only ions which when reacting with higher fatty acids of soap they form an insoluble gelatinous curd, thereby causing a waste of soap. This type of reaction does not take place with other ions.
Regards