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

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.

This script can be expanded on to address some of its limitations.

Challenge: Add code so that when players finished, they can repeat the race by touching the start line.

Challenge: 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.

Challenge: Design a way to give feedback, like a particle effect that plays for winner. To learn how to attach particles to players, see Creating a Reward Part: Giving Player Feedback.

Challenge: 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 Project Sample

Project File

Download the finished project here.

Finished Script