Did guns end the need for monarchy I've noticed that from the 1400s to 1918 with the invention and improvement of firearms monarchies around the world decreased or is it just me?
How is it that monarchy a governing system that has been around for thousands of years nearly get replaced by republics?
Those are two very different question with completely different answers. So let me go for the first one.
And the answers is a resounding no, there is no link whatsoever between firearms and types of government. Let's demistify firearms here because the traditional narrative of the early modern military revolution and its many influences in popular culturs has credited firearms with a mighty power to revolutionize societal structures at a grand level, but current scholarship is much more nuanced on this subject. The conception that firearms did prompt important military and arguably sociopilitical changes has been long around so let's examine the history of the idea.
Traditionally it was believed in times of the enlightement and their modernist descendants of the 19th century that firearms had radically changed the structures of power of medieval society, as the cannons brought down the castles of the feudal lords and the humble musketeers were able to kill them easily in their steel plate armour, thus ending the obscurantist era of feudalism and ushering in the age of reason. While there is some grain of truth to this, this is a flawed understanding of the changes of the period. When it comes to siege warfare there was indeed a period of unbalance in which artillery trains could easily demolish old age castles made of thin and tall walls vulnerable to breaches, but this was later fixed by the development of the trace italienne or bastion fort that was much more resistant to cannon fire and integrated firearms as well for its defense. As for the knights part there was indeed a decline in armoured cavalry during the period (though it was still around), but aristocratic elements of society did not lose their relevance nor their military role, it just changed, as they stopped being warriors owing military service to their lords and instead became officers and generals within the standing armies of the state.
And this leads to the modern interpretation of the military revolution, pioneered by Michael Roberts in 1956 and expanded by Geoffrey Parker in 1988, and given a particular spin by any and all military historians ever since. Basically a ton of major changes did take place during this period, although the relationship between transformations in purely military matters and the broader societal framework in which war occurs is hard to pinpoint. The period from roughly the 15th to the 18th century saw the rise of infantry armies and artillery forrces, the aforementioned trace italienne, the creation of standing fleets as an effective military arm for extending the interests of the state beyond the seas, the formation of standing professional full time armies under direct control of the state, along with the development of large bureaucratic systems to organize maintain this larger military bases including taxation and financing systems as well as increase in the capacity for coercion and control of the monopoly of violence, and much more. Although this has more to do with the military rather than the political side of your question, if you wanna learn more about the concept of the miltiary revolution in Europe I can recommend Jeremy Black's "European Warfare 1453–1815"
This is just a broad overview but for our purposes it suffices to point out that indeed none of the changes that happened during the early modern period, and specifically in terms of firearms, have anything to do with the demise of monarchies, on the complete opposite this period saw the rise of much more centralized and poweful monarchies better able to utilize the resources of the realm for their own purposes and with strategic interests far beyond their own borders. The Spanish and Portuguese empires, followed by their British and French peers, the growing power of the Habsburg Austrians in central Europe and in the Balkans against the mighty Ottoman state, the rise of the Russian empire from beyond the steppe and the Prussian military establishment, all of these polities that would become major European powers during the period were resolutely monarchies that conceived their power as derived from their divine appointment from God and their noble lineage. From this period came the conception of the absolute monarchy, a political paradigm by which the affairs of the state were assumed to be an exclusive prerrogative of the king only, and his power was absolute over other traditional autonomous power structures like the church, nobility and towns. Of course this is in theory as the central government still had to find consensus among compliance among these powerful groups, but only few challenges to the absolutist order in itself occured during the period, such as the Dutch revolt or the English civil wars.
The overturn of the political establishment of the European monarchies that resulted from this period of sociopolitical transformation came not from something as simple as military technology, although several wars against monarchical establishments were fought with muskets and cannons. It would instead come from the intellectual side, as the thinkers of the Enlightenment would lay the groundwork for the rejection of the establishment of the absolutist kings and their alliance with church and nobility, the concept of popular sovereignity and equality before the law, and many other concepts that would form the basis of the modern democratic establishments. The two major historical events that would bring these ideas in full force as real political projects would be the american war of independence and later on the French Revolution. Nevertheless while republics would spring up during the 19th century such as during the Latin American wars of independence or establishment of the second and later third French Republics, and many of the remaining monarchies would become constitutional monarchies with limits on the power of the sovereign and forms of democratic participation, there's no clear decline on the number or power of monarchies, and by the onset of the First World war you still had an Italian, Spanish, Belgian, Dutch, British and Irish, Bulgarian, Serbian, Romanian, Greek, Norwegian, Danish and Swedish kingdoms, and Austro-Hungarian, Russian, Ottoman and German Empires. Not to mention that outside of Europe native structures of power remained only partially influenced by European contact, and even not necessarily in favor of republics, as you had the recently declared Chinese Republic but also the Japanese Empire, and the Persians became a constitutional monarchy but the Thais an absolutist one. If anything the most drastic rise in political systems was that of colonial establishments set up by Europeans throughout the world. I find the radical changes that occured in the world in the 19th century terribly fascinating so i would recommend reading the monumental trilogy of Eric Hobsbawm on the so called long 19th century (1798-1914), or for more modern scholarship Christopher Alan Bayly's "The Birth of the Modern World: 1780-1914" to get a real good picture of the drastic changes that occured during this period, especially relating to the early history of the modern liberal democratic establishment and the deeply interconnected rise of globalized capitalism.
I don't feel terribly confident speaking much on the second question, but many of the modern democracies came in waves, highly influenced by other various political currents such as nationalism, anticolonialism, socialism, and modernization. The end of first world war led to the collapse of all of the aforementioned empires which led to the establishment of several republics such as the polish, czechoslovak or hungarian republics, as well as a few monarchies such as the short lived Syrian kingdom or the Iraqi kingdom. Later on during the period of decolonization following world war two many of the former colonies established themselves as republics, such as the many countries that now compose Africa. Socialist movements also played a part in establishing various socialist regimes in China, North Korea, Vietnam, Cuba, Ethiopia etc. And lastly the collapse of the Eastern bloc led to many of the eastern european socialist regimes turning into liberal democracies