All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad is His slave and Messenger.
The scholars answered this contradiction with many answers and guidelines; perhaps the clearest of these guidelines is what Shaykh Ibn ‘Uthaymeen mentioned that a person does not have anything from the good deeds of another person except if the latter allocates for him some deeds, in which case, these good deeds will be for him. He said in his book "At-Tafseer": “The evidence presented by the scholars who are of the view that the good deeds of one person do not reach another person, like the saying of Allaah: {And that there is not for man except that [good] for which he strives.} [Quran 53:39], does not mean that they cannot reach him, rather it means that the endeavor of somebody is confirmed for him and that the endeavor of another person cannot be for him except if that person allocates something of it for him. This is the same as saying to someone: ‘You do not possess except your money’; this does not prevent him from accepting a donation given to him by others.’’
Allaah Knows best.