Yes, unfortunately, the old joke about "how do you get to Carnegie Hall?" applies to any musician.
Obviously, while we apply the term "muscle memory" to learning the mechanics of playing an instrument, that isn't at all what's occurring as you play the guitar. Unlike the automatic functions of breathing and blinking, your brain has to work fairly hard when you are stringing together a series of musical notes (which constitute a foreign language for anyone) and then translating those blips rapidly in your mind to what those individual blips on the radar screen mean to the overall movement of your hands. Age plays a small part in your success as no one has a brain which is quite as quick and nimble at 50 as it was at 20. Therefore, while you may not yet require crutches to walk, you might want to learn how to use a few when you are learning to play. And keep in mind the science of crutches has become more developed as the rest of the world has demanded better results. That, however, is a response that goes well beyond the scope of a forum.
On a fairly basic level, first, ask yourself a few questions about how you are learning the riff and why you are learning the riff.
If you have approached learning how to play the guitar by learning riffs, then you have missed the portion of your vocabulary lesson where letters are turned into words and words into sentences. You learned to speak a language by using words to form sentences but then sentences grew to become phrases and phrases became paragraphs. If all you've bothered to learn in the language of music is the equivalent to, "Where is the toilet?", then you don't have a clear concept of how to grow words by placing them together to make full ideas. You are relying only on the mechanics of playing and ignoring the thought processes of music.
Which means you have to begin thinking of music as a language. Music speaks to each individual in a somewhat universal language but not everyone knows how to use that unique language to express themself to others. That means you have to begin thinking in terms of using a language and having something to say and knowing how you wish to say it. You can use identical words in many ways - this is a common exercise for beginning acting students - but how you say those words expresses a thought that changes with needs and intent. If you are only thinking of this riff as a few bars and you're done, then it's no great wonder you will have a problem connecting those few bars to a longer, more expressive thought. You need to begin thinking of the language of music as not just a bunch of individual words strung together but as a thought - or a series of thoughts which are connected to one intention - which is being played with an intent behind it.
Most often seen in classical music, many musicians will "mark up" their score. If you are unfamiliar with the idea, learn a few of the ways a musician uses a pencil (always a pencil, because you may change your mind) to place cues and reminders on the page. If possible, work towards reading just a bit ahead of where you are playing so you can notice the cues prior to arriving at the point you need to use them.
Next, work to understand the riff's use and purpose. Musically and mechanically, if the riff is in the key of E Major, then it relates to the notes found in the E Major harmonized scale. A riff in A minor will have different possibilities in it. If you don't know how to harmonize the E Major scale, learn. There is a concept in music theory which is often called "practical music theory for the guitarist" which summarizes the most significant ideas in music theory and gives just those which apply to the non-PHD student of guitar; https://www.amazon.com/HAPPY-TRAUM-GUITAR-METHOD-SHOULDKNOW/dp/1597732893
Knowing in your head what could possibly be ahead is ideal when playing the guitar. Once you comprehend the possible, you can exclude the not possible. Now that you have a road map, you begin to understand where the map begins and where it takes you. You can see (and mark) the curves and the rest stops, the locations to buy gas and food and the danger zones and the speed traps, etc. The locations are marked out for you, you then have to decide on exactly how to use that information. In terms of the music itself, you can see where the notes take you in terms of ascending and descending lines, where there are unison sixths and minor thirds, where bends and rests occur. In other words, learn more than just a string of notes a few bars long.
Ask yourself why you are messing up. If you are trying to play an exact note for note copy of a riff, then you may be playing above your pay grade. Most famous riffs were not created by student musicians. Know when you should be able to play and when you should wait. Decide whether learning only to play what someone else has created is your real goal. If you are "messing up" by allowing your fingers to find other notes which still work within the context of the song, then possibly you aren't "messing up" as much as you are finding creative solutions which make the riff your own.
If all you're doing is learning what someone else has created, you're not really learning to play music. You will find you can't create music on your own and, when the time comes, you can only hope to remember the note for note riff that Jimmy or Eric or BB created. That is once again similar to learning to say, "Where is the toilet?", in a language but not being able to discuss in that language what you will spend the day doing. You will forever remain a tourist in the land of playing music.
In just practical terms, slow down and learn how to use a metronome. A metronome is purely objective and makes no comment on how well you play. It is probably the very best tool a student musician can use. If you don't own a metronome, get one as an app for your digital device and learn how to use it to build your playing level.
Learn how the riff connects to the body of the song. Naturally, I mean learn the way the riff is used musically, but, more basically, just learn how it connects mechanically. There are plenty of results you can gather for any search engine which asks how to use a metronome in your practice routine that will help you here.
Don't skip the rest and go directly to doing only what gets you a result but begin practicing the riff from a measure or two ahead of its location in the song. Put the riff in your head as a part of the song and not just as a stand alone phrase. Mark your score accordingly. Once you have the song a few measures ahead of the riff, move another few measures ahead of that point. Build the foundation of where the riff sits in the song by constantly moving that starting point further ahead in the score. Keep going like this until you have the entire verse in your hands before you go back to play the entire song.
There are more specifics you can address to overcome this issue but those are the most important solutions IMO.