Coming up with melodies for beats has been a struggle of mine for some time, but I've come across a few tricks that have helped me out over the years, so hopefully they'll be of help to those reading this post too!
1. Learn scales
Learning to construct major and minor scales will help out a lot when it comes to creating melodies. Scales give a good guideline to build upon and allow you to do less guess work when it comes to choosing the right notes for a melody. It might feel like a chore taking the time to learn scales, but definitely don't skip this step.
2. Build from chords
If you start a track with chords, a really simple way to add a melody is to use notes from each chord to build your melody. Once, you have down the basic notes that work you can add in extra notes to spice the melody up a little.
3. Hum it out
If you're really getting stuck and it seems like a good melody just isn't coming around, try humming. I can't count how many times this has helped me get a good idea going. Humming a melody idea is a lot more organic than trying to pluck away at keys randomly, and even if your humming idea doesn't translate into the track perfectly it might lead to another idea that works.
4. Don't force it
Sometimes, you have to ask yourself if the beat truly needs a melody to begin with. Us producers often forget that the last instrument on a beat is going be the vocals. Forcing melodies can detract from the overall beat and might distract the artist from adding their own melodic ideas. If a melody isn't coming together, try moving on and continue to work on the other aspects of the beat.