UevKF8zjRV_WEtpJsXhr
We use cookies on this site to enhance your user experience

Multiple Conditions with If, Elseif, And Else Pt1 (Segment)

Multiple Conditions with If, Elseif, And Else Pt1 (Segment)

Sometimes an if statement needs to be able to handle more than one possible outcome. Like in a race, you might want to give out different medals depending on how fast the player finished.

You could write a unique if statement for each medal to award players, but that takes a lot of time. A faster way is to code a single if/then statement, and use the keyword elseif to provide alternative conditions to test for if the first one in isn’t true.

When the if/then statement runs, it’ll start at the top and run the code for only the first true condition it finds. Below is a code comparison with and without elseif. Notice how with elseif, the keyword end only needs to be typed once, making the code much shorter.

Without elseif With elseif
if player won first place then
Get a gold medal
end

if player won second place then
Get a silver medal
end

if player won third place then
Get a bronze medal
end
if player won first place then
Get a gold medal
elseif player won second place then
Get a silver medal
elseif player won third place then
Get a bronze medal
end

Setup the Race Course

In this project, you’ll create a single-player parkour course where a player will get a different medal based on how fast they finish. Start by placing the starting point and finish line for the course, and then create a script to time the player and award different medals.

Create Parts and Scripts

  1. Create an anchored part named FinishLine with an attached script named ParkourScript. To make testing faster, place the start and end close together. You can move the finish line after finishing the script.
  1. In ParkourScript, create two variables that:
  • Hold how many seconds have passed since the race started.
  • Store the finish line part.
  1. Create a new function named finish() with a print statement to test the code later.
  1. On your own, code a function named partTouched() that runs finish() when a player touches it. Don’t forget to connect the finishline's Touched event to partTouched.

  1. Playtest and check that finish() is called when you touch the finish line.

Stop finish() From Repeating

Right now, whenever a player touches the finish line, finish() gets continuously called as long as the player is touching the part. Use a boolean, a variable that stores true or false, to make sure that finish() is only called once.

  1. Create a new variable named raceActive and set it to true.
  1. Add a second condition to the if statement to check if raceActive is true before calling finish().
  1. To stop finish() from being called again, set raceActive to false.
  1. Playtest your game to check that you only see your test print statement once.

Keep Track of Time

Like an if statement, a while loop can also use a condition to see if it should run. To time the player, create a timer using a while true do loop that only runs when the raceActive variable is true. Other while true do loops used previously didn’t have a condition, so there was never anything stopping them from running forever.

  1. At the bottom of the script, type while raceActive == true do.
Make Sure The While Loop is at the Bottom

Even though this while true do loop eventually stops, it should still stay at the bottom of the script. This ensures that the previous code runs before it reaches the loop.


  1. To time the players, in the loop, add 1 to the timePassed variable once every second. Include a print statement to test your work.
  1. Play the game and check that you see each second displayed in the Output Window.

Awarding Medals to Players

Create an If Statement

Use an if/then statement with two elseifs to check the player’s finish time and award them the correct medal.

  1. Since you’ve tested that finishRace() works, remove the test print statement. Every line of code takes time to run, so removing extra code helps make your game run faster.
  1. To better see what medal is awarded for what time, code a print statement that includes timePassed. To combine a string with a variable or other strings, a process called concatenation, type .. between the string and the variable name.
  1. To check if the player earned a gold medal, code an if statement that compares timePassed to the fastest you’d expect a player to finish. This example checks if the players time was less than or equal to 10 seconds.
Keep Code Indented

To make your code easier to read, check that your code is indented like the block above. To indent your code:

  • Tab to move a line forward.
  • Ctrl / Cmd + Tab to move a line back.

  1. Playtest and check that you can receive the gold medal.

Add ElseIf Conditions

Now that you’ve tested for the gold medal, code conditions for the other medals using the elseif keyword.

  1. For the silver medal, type elseif followed by the range of time the medal should be earned. In this example, the range is greater than 10 seconds but less than or equal to 20 seconds.
Finish the elseif with then and add a print statement on the next line.
  1. See if you can figure out how to award the bronze medal. Check your code with the example below.