Can you have a script run X amount of times? (equal to a variable)

So the title explains it all. I'm trying to spawn a random amount of ore equal to a variable when the player enters the area. I even have the events to handle it set up how I THINK it should work, but its not. The problems I'm currently having is I keep doing small changes to see if anything will work but either A: Nothing spawns, or B: the game freezes after loading the area.
Currently I have 2 events handling the spawning, I tried doing it in 1 event but wasnt working either, so this is just my current setup.
1698095594855.png

This event sets a random variable and sets the OreSpawns variable equal to 0 (for reuse in the future,) then turns itself off (the second page is just empty.)
1698095717397.png

This second event is supposed to spawn the events if the orespawns variable is less then the random variable. Then when it is, break the loop and switches to another empty page. (The reason why the wait is at the top is I wasnt sure if running the first and second event at the same time was causing issues trying to set variables at same time but didnt change anything.)

I've been trying a while to get this to work and I'm hoping maybe someone that knows more then me can point out any issues, or maybe It'll just help to get another pair of eyes on it to spot whats wrong.

SIDE NOTE: I'm using galvs event spawner plugin to spawn the ores at random positions in a given region, that's what the script is.
you checked the wrong condition.

If orespawn > random
starts with
if 0 > 10
which will never be true, and so the counter never increased and spawn never happen

you need to check lesser, not greater
On top of what Andar said, the "Control self-switch" in the second screenshot will never happen, since you break out of the loop just before it. You'd probably want to put it before the "Break loop" command.
and another problem: both are autoruns, but only one autorun can be processed at the same time.
so it would be better to combine the commands into a single event instead of hoping that the first event runs and sets the variables before the other event
you checked the wrong condition.

If orespawn > random
starts with
if 0 > 10
which will never be true, and so the counter never increased and spawn never happen

you need to check lesser, not greater
I audibly facepalmed that I didn't notice this. Thanks for pointing it out!

Right after I also had a moment of clarity and realized I can just put the spawn event on page 2 of the event that set variables to make it run after, and I can just make it a parallel trigger that turns itself off instead of using loops. I was severely overcomplicating things. Guess that's what happens with lack of sleep.

It all works perfectly now though thanks a lot to both of you for the quick replies!

Original thread: https://forums.rpgmakerweb.com/threads/can-you-have-a-script-run-x-amount-of-times-equal-to-a-variable.162320/