From an intertextuality perspective and because Jesus and the young ruler were both Jews, I believe the background of this passage is Deuteronomy 8:
[Deu 8:1 NLT] (1) "Be careful to obey all the commands I am giving you today. Then you will live and multiply, and you will enter and occupy the land the LORD swore to give your ancestors.
However, Jesus is the all-penetrating LOGOS of God the "searches men's hearts" (and other organs) representing their thoughts and intentions and prophetically reveals them and so wins the lost:
[Jhn 4:29 KJV] (29) Come, see a man, which told me all things that ever I did: is not this the Christ?
[1Co 14:24-25 NLT] (24) But if all of you are prophesying, and unbelievers or people who don't understand these things come into your meeting, they will be convicted of sin and judged by what you say. (25) As they listen, their secret thoughts will be exposed, and they will fall to their knees and worship God, declaring, "God is truly here among you."
[Rev 2:18-23 NLT] (18) "Write this letter to the angel of the church in Thyatira. This is the message from the Son of God, whose eyes are like flames of fire, whose feet are like polished bronze: (19) "I know all the things you do. I have seen your love, your faith, your service, and your patient endurance. And I can see your constant improvement in all these things. (20) "But I have this complaint against you. You are permitting that woman--that Jezebel who calls herself a prophet--to lead my servants astray. She teaches them to commit sexual sin and to eat food offered to idols. (21) I gave her time to repent, but she does not want to turn away from her immorality. (22) "Therefore, I will throw her on a bed of suffering, and those who commit adultery with her will suffer greatly unless they repent and turn away from her evil deeds. (23) I will strike her children dead. Then all the churches will know that I am the one who searches out the thoughts and intentions of every person. And I will give to each of you whatever you deserve.
So that's the why. But as we read on we see the method to his "madness":
[Luk 18:20-21 KJV] (20) Thou knowest the commandments, Do not commit adultery, Do not kill, Do not steal, Do not bear false witness, Honour thy father and thy mother. (21) And he said, All these have I kept from my youth up.
Did you notice what was missing? "Do not covet"! Coveting, as I define it, is trusting in money. Many of us may consider sexual temptation as the biggie among "sins" but coveting seems to have been one that tripped up many in Jesus' day:
[Rom 7:7-11 ESV] (7) What then shall we say? That the law is sin? By no means! Yet if it had not been for the law, I would not have known sin. For I would not have known what it is to covet if the law had not said, "You shall not covet." (8) But sin, seizing an opportunity through the commandment, produced in me all kinds of covetousness. For apart from the law, sin lies dead. (9) I was once alive apart from the law, but when the commandment came, sin came alive and I died. (10) The very commandment that promised life proved to be death to me. (11) For sin, seizing an opportunity through the commandment, deceived me and through it killed me.
So the "one thing" that he lacked was to go to Covetless School:
[Mat 19:21-24 KJV] (21) Jesus said unto him, If thou wilt be perfect, go and sell that thou hast, and give to the poor, and thou shalt have treasure in heaven: and come and follow me. (22) But when the young man heard that saying, he went away sorrowful: for he had great possessions. (23) Then said Jesus unto his disciples, Verily I say unto you, That a rich man shall hardly enter into the kingdom of heaven. (24) And again I say unto you, It is easier for a camel to go through the eye of a needle, than for a rich man to enter into the kingdom of God.
Note: This was not idle talk. Jesus did call people to forsake all and they did. But apparently the young ruler would not part with his earthly treasures so he would forego his heavenly treasures.