Multiple Conditions with Elseif and Else

15 min

Multiple Conditions with Elseif and Else

Learning Objectives Students will be able to:
  • Understand the benefits of using if, elseif, and else statements as opposed to if statements for individual conditions.
  • Create a script using if, elseif, and else that awards different medals to players based on how fast they complete a parkour course.
Prerequisites Students should:

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

if player won second place then
Get a silver medal

if player won third place then
Get a bronze medal
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

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.

Keep ElseIf in Scope

An if statement can have many elseif conditions, but they all must be coded between the first line of the if/then statement and the if/then statement’s end.

  1. Test for the silver and bronze medals.
  • Each elseif should have a then after it’s condition. Example: elseif timePassed > 10 then.
  • In partTouched(), make sure the second condition of the if statement uses ==, like in raceActive == true.
  • Check that each elseif is in scope. Each elseif condition must be between the first line of the if/then statement and it’s last end.

Check using Else

If the player didn’t earn any of the medals, you should encourage them to try again. In this case, you can use an else statement, which runs if no other conditions were true, to show them a message.

  1. Below the last elseif and above end, start a new line and type else. Do not add then. Beneath else, use a print statement to prompt them to try again.
  1. Playtest to see the "Try again!" message.
  • Check that your else statement does not have a condition, like timePassed <= 20 or a then after it.
  • An else statement should always be the last check in an if statement. Check that the else statement does not have any other conditions, like if, elseif, or else under it and the end of that original if statement.

Try a challenge to expand on the script.

  • Add code so that when players finished, they can repeat the race by touching the start line.
  • Design a way to display time during a race. You can either display the time on a part using a Surface GUI, like in the Creating a Timed Bridge tutorial, or check out the Intro to GUIs article to display the timePassed variable in a TextLabel.
  • Modify the script to use a table (see Creating and Using Tables) to keep track of all players when they join a game. Instead of using the raceActive boolean, give each player a boolean value instead to track whether or not they’ve finished the race.

Finished Script