Indonesia is an Islamic State today, but before, it was Hindu-Buddhist. Bali is the only island with a majority-Hindu population. Why?
Maritime networks shifted Mahajapit vassals to Islam, moving it away from the Hindu-Buddhist faith. Islam had an advantage over other religions, because it was founded by merchants! Converting places to Islam only required books, while converting places to Hinduism or other religions required temples, which were much costlier. So in the 13th century, many locals in Indonesia began converting to Islam. Why? Well, we know that many Mahajapit vassals and enemies may have wanted some level of secure autonomy. This meant that those kingdoms would look to foreign powers for security. Before this, they would either look to Hindu India or Buddhist China for help. This is why Indonesia used to be Hindu-Buddhist. But in the 13th century, Islam was accepted in China under the Mongols. Since China was weakened by the Mongols, the Thai Empire and the Mahajapit Empire could both gain land and become strong without being a Chinese puppet. Islam also brought advanced Arabic writing, paper, and, most importantly, good relations with Muslim powers in India. This meant that the vast knowledge from the science of Hinduism and Buddhism could be translated to Arabic, and combined with the science from the Arabic World. And even more importantly, nations that wanted autonomy from the Mahajapit empire looked to Indo-Muslim powers or China, where the Mongols allowed Muslims to live. Later on, Zheng He was one of the main Chinese explorers who went on diplomatic missions into Southeast Asia. He was Muslim as well.
So now the options for most kingdoms in Southeast Asia were this: Either stay as part of the Mahajapit empire(which was in a civil war), Declare themselves not loyal to the Mahajapit rule and then get crushed by Mahajapit navies, or convert to Islam, gain the favor of INDIA AND CHINA, learn Arabic knowledge, and then declare yourself independent. The Mahajapit empire can't do anything because you have two strong powers on your side.
In addition, Ibn Battuta was in Indonesia, and that helped Islam to spread into Indonesia.
Anyway, the year was 1398. The great Mahajapit empire was very strong and it indirectly controlled much of Indonesia. It sent ships to conquer the weaker kingdom of Singapore. Why? Because Singapore was the last bit of their old, strong rival Srivijaya. All the last remains of Srivijaya were taken to Singapore. Singapore is one of the example cities for converting to Islam. They had the expansionist Thai kingdom to the north to worry about as well. So while Singapore was being invaded, the king of Singapore (Srivijaya) fled to a strategic location. He converted to Islam, to gain support of India and Zheng He, and collected taxes from traders. He was right on the Strait of Malacca so he was prime to get rich from trade. He called his new empire the "Sultanate of Malacca". After getting a secure relation with China and India, he started to spread his influence through Indonesia.
The deal between China and the Sultanate of Malacca was this: China would get a check on Thai and Mahajapit power, and Malacca would get a strong navy and be secure. The deal between India and other Muslim powers and the Sultanate of Malacca was this: Convert to Islam, and I'll make sure I buy from you, paying taxes when I trade.
Like this, many kingdoms freed themselves of Mahajapit rule and converted to Islam. These Muslim kingdoms began getting powerful. Mahajapit power began falling, and on top of that, the civil war hurt Mahajapit even more. Mahajapit was the last Hindu-Buddhist kingdom in Indonesia. They began losing money as the trade in Indonesia was shifting to Muslim powers. Eventually, Sumatra rebelled, and all Mahajapit controlled now were Java, Bali, the Spice Islands, and various small islands. However, eventually, Java itself was captured by a coalition of various Muslim kings. Now, Mahajapit was basically dead, as the Spice Islands had changed sides. Then, Mahajapit surrendered, but gave their culture to Bali. Bali continued to be a Hindu-Buddhist Island to this day, and still preserves old Mahajapit culture. Mahajapit theatre was one of the greatest forms of art, and Mahajapit history eventually became the spark that created the fire of Nationalism in Indonesia, which stopped the imperial Dutch rule centuries later. The reason is clear: The rest of Indonesia converted to Islam to get protected by Muslim kings in India and protected from Zheng He so that they can easily gain independence from the Mahajapit Empire. Bali was the last island under Mahajapit rule and it continued its Hindu practices far after the fall of the Mahajapit Empire.
The people in those kingdoms followed whichever religion the king was. If they did not want to convert to Islam, many moved to Bali where they could freely be Hindu.
Hope this answers your question.