Using magnetic endohedral fullerenes for molecular spintronics requires control over their encapsulated magnetic moments. We show by field-dependent x-ray magnetic circular dichroism measurements of Gd3N@C80 endohedral fullerenes adsorbed on a Cu surface that the magnetic moments of the encapsulated Gd atoms lie in a 4f7 ground state and couple ferromagnetically to each other. When the molecules are in contact with a ferromagnetic Ni substrate, we detect two different Gd species. The more abundant one couples antiferromagnetically to the Ni, whereas the other one exhibits a stronger and ferromagnetic coupling to the substrate. Both of these couplings to the substrate can be explained by an indirect exchange mechanism mediated by the carbon cage. The origin of the distinctly different behavior may be attributed to different orientations and thus electronic coupling of the carbon cage to the substrate, as revealed by scanning tunneling microscopy of the fullerenes on Cu.