In the initial moment of military conflicts between African states and European imperial forces from about 1870-1910 (different times and scales at different places), African forces generally tried to leverage two major advantages to good effect: numbers (and the supply lines to support them) and familiarity with terrain.
Those advantages worked for a time to delay or defer European forces--most famously in the opening battle of the Anglo-Zulu War in 1879 at Isandhlwana, but also in the last major Anglo-Asante War, in a number of conflicts in French West Africa, and successfully to some extent in the first conflict between Italian forces and Ethiopian forces. Prior to the late 19th Century, in fact, these tactics were frequently sufficient to force intruding European forces to withdraw entirely after only limited military success (as in earlier Anglo-Asante conflicts or in the Anglo-Xhosa wars in South Africa) both because European forces lacked the military capacity (and political desire) to wage sustained wars of occupation past the coastal forts and settlements established in the early modern period and because those tactics were effective in their own right.
The change in European military capacity at the end of the 19th Century was important in making those tactics ineffective. It wasn't always a matter of guns vs. non-guns. In some cases, African forces were armed with firearms as well as locally produced weaponry, but generally their firearms were mid-19th Century or so at best--often muzzle-loading rifles or muzzle-loaders converted somewhat inefficiently to breech-loading. More important than specific rifles or weapons like the Maxim gun, however, was the vast increase in the ability of European states like France and the UK to support sustained operations by well-armed troops even in unfamiliar or inimical environments--to bring those troops more rapidly due to changes in transportation, to command them more directly from the center of imperial power due to instantaneous global communications, to keep them armed and fed and more protected from endemic diseases like malaria over a longer period of operations. You could make an argument that the force asymmetry of the late 19th Century and early 20th Century in most of sub-Saharan Africa was as great as it has ever been in world history between two clusters of human societies and in so doing you could note that it was an incredibly brief window between the relatively equal balance of force up to the end of the 18th Century and the relatively equal balance of force that emerged by the 1960s.
African societies that tried to rebel against colonial occupation after the initial conquest continued to try and leverage numbers against much smaller imperial military forces--sometimes through trying to inspire large numbers of men to attack despite countering fire from repeating rifles in order to overwhelm imperial lines, as in the Maji-Maji uprising against German authorities in colonial Tanzania. Increasingly this simply didn't work as imperial militaries became more adept at preparing for and countering such attacks and as colonial authorities moved to break up or punish anything that looked as if it might mobilize organized military resistance (often harshly punishing people involved in political or religious movements that didn't necessarily intend to mount a violent or even non-violent challenge to imperial domination). Harsh reprisal tactics that amounted in some cases to genocide (as in German reprisals against the Herero in colonial Namibia) also affected the willingness of African societies to continue mounting direct military responses to imperial authority.
In addition to what u/swarthmoreburke has written, it's worth mentioning the Ethiopian approach in more detail - an approach that kept them free of colonial domination into the 20th century.
The Ethiopian approach: buy all the guns and cannons you can, to match European firepower.
European powers were not the only target of this. Ethiopia had fought the Sudanese Mahdists in one of the last large predominantly spear-and-sword battles in the world. Both sides used guns, in limited numbers (but in sufficient numbers for the Ethiopian emperor, Yohannes IV, to be killed by a musket ball). European powers, who had at first encouraged this arming (the British in order to stop the Mahdists), tried to stop or at least restrict this flow of guns. Germany refused an order for 100,000 rifles, and Italy stopped direct imports. Imports continued through Djibouti, perhaps about 25,000 rifles per year. When the Italian and Ethiopian armies met at Adwa (Adowa) in 1896, the Ethiopian army brought more guns to the battle: about 80,000 vs 15,000.
For more on the 19th century arming of Ethiopia, see my answer in https://www.reddit.com/r/AskHistorians/comments/970jd7/the_ethopian_army_during_the_first_italoethiopian/