I've been writing music and lyrics for a few years. Lyrics are a breeze, but as for the music...
I can only "write" what to me sounds good. By this I mean what notes seem to fit with the others with little to no theoretical knowledge. My bandmates (who do all the writing thankfully) think the songs sound great, and even sometimes ask how I wrote or put riffs together. And my only response being, "I just write what sounds good."
I would like very much to understand my own writing techniques, but more importantly expand my ability to write, so I've dove into studying musical theory.
With some of that under my belt by means of a keyboard, I have a general idea about the information within the circle of fifths, and learned how to write/read staff.

Getting to the point of my story, I was wondering if I can get a "point into the right direction" of where to start writing with some theoretical knowledge. Do I need to decide what key I'm going to write in beforehand? Could I write a riff, determine it's key and continue writing? What about the exceptions?
Basically, what should I learn first? Scales? Modes? How do I apply this knowledge?
Or is any of this even close to where I should begin? :-P

Any comments MUCH appreciated, thanks UG!
Do what feels right and sounds good? I always learn scales first myself.
Custom Fender Strat
Oscar Schmidt Acoustic
VK 112
Epi VJ
EHX Holy Grail
Ibanez DE7 Delay
Modded Tubescreamer TS7 -> TS-808
Strat Knockoff
you would be surprised how many bands/artists are very unfamiliar with the theory aspect of writing....if it sounds good thats all that matters if you ask me.

in terms of pointing you in the right direction, there are a number or articles on this site that discuss writing and theory, Id take a brows through and start with those for now

Epi Les Paul Std w/Duncans
Cry Baby From Hell
Marshall JH-1
EHX Metal Muff
MXR EVH Phase 90
Carl Martin Classic Chorus
EHX #1 Echo
Ibanez LU-20
Dunlop DCB-10
Crate V50112
Tascam US144

Check out the Ultimate Guitar to Guitar it has everything you need to know and it's on this website it's awesome.
I do it this way: I play what I think that sounds good, write it down, afterwards think about what I've written and try to draw conclusions from it.

But somehow it seems that I do this everytime so I don't know if the method is that good. At least it's a method and it may work for you