Most keyboards have mechanical switches, and as such are designed for a maximum number of "throws" before they fail. The spacebar is the most-used key on the unit, so that's the one most likely to go first.
30 years ago you paid a hundred bucks or more for a keyboard and they lasted forever. Nowadays, they're 10 bucks and stamped out by a machine and intended to last about as long as an iPhone model.
When a microswitch starts to fail, it usually does so intermittently at first. It either sporadically refuses to transmit a keypress at all, or it transmits a dozen every time you press it. The spacebar often has two switches due to its width, so one may fail before they other, such that whether or not it works depends on which hand you hit it with.
Bottom line: you may just need a new keyboard. Got any old ones in the closet from retired computers? Sometimes an ancient keyboard is more reliable just because it was built to higher standards.