Because of the advice of the Midianites, they hired Balaam. When they saw that Balaam with his evil eye couldn’t harm Moses and the Israelites, they sent their wives and daughters with a secondary play.
“הֵן הֵנָּה הָיוּ לִבְנֵי יִשְׂרָאֵל בִּדְבַר בִּלְעָם לִמְסָר מַעַל בַּיהוָה עַל דְּבַר פְּעוֹר וַתְּהִי הַמַּגֵּפָה בַּעֲדַת יְהוָה”
“They were the ones who followed Balaam’s advice and enticed the Israelites to be unfaithful to YHVH in the Peor incident, so that a plague struck YHVH’s people.”
The Midianites with their leader decided to send his beautiful daughter to trap Moses. They used witchcraft to made special spells so she can get Moses to fall for her. Their plan failed again because of God’s protection.
“מֵפֵר אֹתוֹת בַּדִּים וְקֹסְמִים יְהוֹלֵל מֵשִׁיב חֲכָמִים אָחוֹר וְדַעְתָּם יְסַכֵּל”
“Causing the omens of boasters to fail, Making fools out of diviners, Causing wise men to draw back And turning their knowledge into foolishness,”
They saw with their magic that a head of the Israelites nation will fall together with thousands others. Their could be sure about the person but thought that it would be Moses. They instructed the woman, Kozbi, to have sex only with Moses. She asks about how to recognize Moses and they told her that if she sees a man that everyone stands for him with respect then he’s the one she should have sex with.
When she came to the camps of Israel, she saw Zimri, the head of the tribe of Shimon and 24 thousand of his tribe were standing before him. She thought that it is Moses and when she came close to him he couldn’t resist and had sex with her. The rest of the tribe saw his actions and followed him with the other Midianite women. That caused the plague that killed 24 thousands. The Midianites knew that if they could connect to the Yessod of Moses, they would be able to drain his energy and the energy of all Israelites. Because the plan was corrupted by God, the plague started at a lower level and could be stopped as Pinchas did.