You are a genius for learning this so quickly! Sulphur has $$6$$ electrons in its outermost orbit. Instead of giving away $$6$$ electrons, it is easier for it to accept $$2$$ electrons. So, its valency is $$2$$.
Magnesium has $$2$$ electrons in its outermost orbit. So, just by giving away $$2$$ electrons, it will have $$8$$ electrons in its outermost orbit. Thus, its valency is $$2$$.