https://en.wikipedia.org/wiki/French_Revolutionary_Wars
The peace of Basel, getting Spain out of the first war against France in July of 1795, just after it lost a crushing defeat near the Border with France at Black Mountain, leaving Spain open to a major invasion by france.
https://en.wikipedia.org/wiki/Peace_of_Basel
The Treaty of San Il De Fonso in August of 1796 put Spain into the war on the side of France. The Spanish feared reprisals from the French for having fought against them earlier. Convinced the French were about to win victory against their enemies, the Spanish went over to the French side.
https://en.wikipedia.org/wiki/Second_Treaty_of_San_Ildefonso
This put Spain at war with Great Britain in alliance with France. When France failed to win quickly, the British rather quickly severed the maritime links between Spain and it's colonies. Critically, in 1797 the British won the battle of Cape St. Vincent. This destroyed the bulk of the Spanish Navy, and allowed the British to almost completely sever all trade and communications between Spain and it's American colonies.
https://en.wikipedia.org/wiki/Battle_of_Cape_St_Vincent_(1797)
https://en.wikipedia.org/wiki/Anglo-Spanish_War_(1796–1808)
There was a peace/truce from 1802 to 1804, when Spain was at peace, and the wars of French Revolution were over. Communcations with the colonies, which to this point had all remained loyal was re-established. War with the UK, as an ally of France re-started in 1804 - now renamed the Napoleonic wars. . Almost immediately in 1805, the British crushed the combined French/Spanish fleet at Trafalger, once again severing Spanish communications with the colonies. The Spaniards were forbidden from cooperating with their colonies via the continental system that Napoleon created. The Spanish flouted the continental system in order to try and keep their colonies alive, and the British let them, in hopes of prying the Spanish away from the French. In the spring of 1808, having defeated a variety of other land based enemies in the East, Napoleon realized that his continental blockade was innefective at crushing Britain, due to the fact that Spain was ignoring the rules, and Portugal never agreed to them. A rapid invasion of Spain and Portugal by Napoleon ensued, and the Spanish army crumbled. Napoleon overthrew the king and queen of Spain, and installed his nephew as king of Spain. The colonies of Spain, immediately revolted, because they stayed LOYAL to the orginal king and queen of Spain. This state of affairs lasted until 1812, when the Kingdom was restored. From the time that Ferdinand the VII became king in 1814, he made a series of fatal errors. He more or less assumed he could re-create the status quo ante-bellum in the Spanish colonies, when in fact the 15 years or so of sustained self reliance by the colonies, and the greatly weakened financial and military capacity of the government of Spain after two massive nationwide campaigns and a 5 year long guerrilla war against French occupation meant that the balance of power between the colonies and the home country had changed dramatically and irreversibly.
https://en.wikipedia.org/wiki/Latin_American_wars_of_independence A quick review of the new/old 'King' of Spain might give you some idea as to why the colonies refused to be ruled by him:
https://en.wikipedia.org/wiki/Ferdinand_VII_of_Spain