The idea that by performing commandments not for their own sake, a person comes to perform them for their
own sake is not only a possibility, but rather it is a certainty, since it is guaranteed that he will ultimately repent.
Although the Torah studied not for its own sake will not connect on its own to God, it will return to Him when the person repents and studies it again for its own sake.
The underlying assumption is that in the end, every person will repent, whether in this lifetime or in another.
That is because the Sages said, “A person should always engage,” and so forth, and if always (all his days) a person studies Torah not for its own sake, when will his rectification occur?
Even if he does not attain proper repentance in this lifetime, then in a different lifetime, in a different incarnation, he will certainly repent properly.
Then all the Torah he had studied, including his study in previous lifetimes, will be rectified.
Rabbi Adin Steinsaltz