Hi ,
Press Windows + G > select settings > Capture, check the option "Record in the background playing game".
Restart your PC and test.
If you have an Nvidia GPU, I recommend recording using the Nvidia app (Alt+Z).
If you have an AMD GPU, I recommend OBS (Optical Screen Recorder), it's the best option.
Give feedback if the information was useful or not.
Thanks