I really like Josh Skaja’s take on this…basically, it’s all about chords, not scales.
I can play major and pentatonic minor scales, but it has not really helped me figure out and play melodies.
But if I know the chords, I am starting to be able to find the melody or riff that is buried within…usually by ear.
And if you check out the link, take some time and browse some of the other topics on his Substack as well. I find Josh’s content to be highly complementary to Justin’s.