Creating the main game loop

In Potato Rush, potatoes fall from the sky indefinitely, and players must collect as many as possible.

First, let's achieve the "indefinite" part. For this, we can use a while true loop, which we learned about in Loops, and this will become our main game loop.

while true {
    wait(1)
}

This will create a while loop that runs forever, and a single execution takes a second.

Now let's integrate potato spawning into the loop. We will have a function called SpawnPotato responsible for spawning a potato at a specified (as an argument) position.

Since the function won't handle creating the position, we will have to do it in the main loop. We can use math.random() in combination with Vector3.new() to generate a random position:

function SpawnPotato(position) {
    -- We will finish this in the next step!
}

while true {
    wait(1)
    position = Vector3.new(math.random(-100,100), 100, math.random(-100,100))
    SpawnPotato(position)
}

The X and Z coordinates will be picked at random between -100 and 100, but the Y coordinate will always be 100. This is so the potatoes get spawned way above the default baseplate, causing them to look like they're falling out of the sky.


This is all for the main game loop. On the next page we'll finish the function that creates potatoes.

Last updated