When following music, some notes are not played as the line is named such as the line of D. In some scales a note on the line of D is played as D# and this is determined by the scale that the music is set in.
In the example shown are two small pieces of music.
The top section is a first draft of a piece of music I composed. It has no time signature and no key signature. The second piece of music has both a time and a key signature.
The first one is my rough first draft, I knew the speed and the scale as I was working on it so I didn’t bother to put them in. If I gave the music to someone else to play they would not know how fast or slow to play, or what key the music was set in.
The second section shows the time signature, so now, the speed of the music is known. The sharps on the first section, signify the scale of the music. (D Major)
Now if you look at the sharps on the lines, you will know which notes must be played sharp. Now, if I don’t know my scales I will constantly have to keep looking at the key signature, to see if the next note is as written or to be played sharp (#) My playing will be stunted and certainly at the very least not smooth.
Assuming I know the scale I never need to look at the key signature again. I know which scale I am playing in. I automatically know which notes should be played half a semitone higher or sharp(#)