Hey i was wondering how do you get those riffs that go exactly like the lyrics. One example would be in the solo of Casey Jones where you can just hear the lyrics, even though there arent any, going "drving that train high on cocaine casey jones you better watch your speed". This is the second part of the solo.

So my question is how do i learn how to write riffs melodies like this? is there any technique or way to figure this out or do i just have to play around until i finally figure it out?

I have found that when I am crafting the music into a song that I have written. I find the melody with the instruments. On piano, guitar, bass, and anything else that plays in a key. You can pretty much pick out the melody and play it on something to the time of the song and you will hear the lyrics. Thats my system. If your newer to writing and playing. Hold off, until you feel comfortable searching deep within the song to find things like that. Just write and get better and doing what you do.