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

This project will use conditional statements to create a part that will give or subtract points on a leaderboard depending on what color the part is when touched. If blue, then it’ll give players a few points. If green, then it’ll give a lot of points. Finally, if red, then it’ll take away points.

Create a Leaderboard

To setup this project, you’ll need a leaderboard to track the points and a part that changes colors. Start with creating the leaderboard.

  1. Create a new script in ServerScriptService named Leaderboard.
  2. Copy and paste the code below into the Leaderboard script.

Create the Color Changing Part

The RGB values for blue, green, and red are on the right.

Each color and point value will be stored in a separate variable. The variables can then be checked to give or subtract points.
0, 0, 255 Gives a few points
0, 255, 0 Gives many points
255, 0, 0 Subtracts points

Make Variables for Each Color

  1. Create a part named PointPart with an attached script named PointScript.
  2. In PointScript, use script.Parent to refer to the part.
  1. Create a variable named blue with the matching RGB like below. This will give players the least points.
  1. On your own, set up the green value (0, 255, 0) and the red value (255, 0, 0).

  1. Add variables for a small amount of points, a larger amount of points, and a third for removing points.

Add the Players Service

To award points, you'll need to get access to the player's information which is stored in the Explorer under Players, and is seperate from the character object. This is where information like leaderboard stats can be found.

You can do so by adding the Players service to your script. Services are additional sets of pre-built functions made by Roblox engineers to save you time.
  • Get the Players service by typing: local Players = game:GetService("Players")
Naming Service Variables

While other variables start lowercase, any service, like Players service, starts capitalized to let you know that variable is a service.


Set Up Touch and Points Functions

PointsScript will need two functions. The first function will give and subtract parts. The second function will check if a player has touched the part.

  1. Create a new function named givePoints() and a parameter named player. Inside, add a print statement to use for testing.
  1. Under that, create a second function named partTouched() with a parameter named otherPart.
  1. Inside the function, use GetPlayerFromCharacter(otherPart.Parent) to look for what player touched the part.
  1. If a player touched the part, it’ll be stored inside the player variable. If not, the variable will stay empty. On your own:
    • Inside the function, check if player has a value. If there is, then call givePoints(player).
    • Beneath the function, connect partTouched() to pointPart's Touched event.
    • Playtest and check for your testing message.

Troubleshooting Tips
  • Check that the "Players" in game:GetService(“Players”) is capitalized and in quotations.
  • Make sure that the Touched connection is at the bottom of the script.

Create Looping Colors

To loop through colors, the script will use a while true do loop that changes the part’s color every few seconds.

  1. At the end of the script, create a new while true do loop.
Why Put the Loop at the Bottom?

If the while true do loop is not at the bottom of the script, any code below it will never be run. Since the while loop doesn’t stop, it’ll keep running the loop instead of any code below it.