This is an easy comma rule:
When you have two complete sentences separated by a conjunction (and, but, or, for, yet), put a comma before that conjunction:
(I wanted to go to the party), but (I wasn’t invited).
However, if what follows the conjunction isn’t a complete sentence, don’t use a comma:
(I wanted to go to the party) but (wasn’t invited).