A person is rewarded for what they intended even when the unintended happens
One day I went to the Prophet ﷺ with a complaint. My father Yazid had taken some gold coins for charity and kept them with a man in the mosque (to give them to the poor) But I went and took them and brought them to him (my father). My father said, "By Allah! I did not intend to give them to you. " I took (the case) to Messenger of Allah ﷺ . On that Messenger of Allah ﷺ said, "O Yazid, you will be rewarded for what you intended. O Ma'n, whatever you have taken is yours."
وَكَانَ أَبِي يَزِيدُ أَخْرَجَ دَنَانِيرَ يَتَصَدَّقُ بِهَا، فَوَضَعَهَا عِنْدَ رَجُلٍ فِي الْمَسْجِدِ۔ فَجِئْتُ فَأَخَذْتُهَا فَأَتَيْتُهُ بِهَا۔ فَقَالَ: وَاللَّهِ مَا إِيَّاكَ أَرَدْتُ فَخَاصَمْتُهُ إِلَى رَسُولِ اللَّهِ ﷺ فَقَالَ: «لَكَ مَا نَوَيْتَ يَا يَزِيدُ وَلَكَ مَا أَخَذْتَ يَا مَعْنُ۔»