At the time the firearms were not rifles because they didn’t have rifling, the spiraling grooves that increase the accuracy of the bullet. As a consequence if you just had a single firearm and fired it at a distant enemy, chances are you would completely miss your target. Massing infantry together was partly intended to make their firing effective through sheer volume; *someone* would hit *something* when you have an entire line of people firing at once.
Another thing to consider is that such weapons were very slow to reload. It was expected that a standard infantryman could fire their weapon twice a minute, and a good one perhaps three times. If you have a lone infantryman in a field shooting once every 30 seconds with horrible accuracy then it is relatively easy to just send a few guys over to stab them with a spear or whatever. By keeping the infantry together in a big bunch they could use their long firearms with bayonets to function like traditional spear formations, deterring people just charging them with melee weapons.
Finally it was likely a requirement of organization and coordination of military units to keep them together in some kind of orderly bunch. There weren’t radios or anything and some nobleman shouting orders to a bunch of peasants free to hide in ditches or scatter into the trees was likely to end up with half the force deserting and the other half not firing a shot. Keeping them together, in formation and performing a synchronized task probably got better results overall. Until firearms actually got accurate enough that was suicide of course.
Latest Answers