We use cookies on this site to enhance your user experience

This project will create a point giving part that is constantly changing colors. Depending on what color the part is when touched, it’ll either award points to players or take them away. Blue will give players a few points, green will give a lot of points, and red will take away points. To set up the different conditions for each color of the part, you’ll create an if statement using elseif and else.

Setting up the Project

To setup this project, you’ll need a leaderboard and a part that changes colors.

Create a Leaderboard

On your own, create a leaderboard script to keep track of player points. If you haven’t created a Leaderboard before, learn how to create one with this tutorial.

Create the Part and Script

Unlike the other looping part tutorials, this script will store each color in it's own variable to figure out how many points to award players later in the script. The points value for each color will be stored in separate variables.
0, 0, 255 Gives some points
0, 255, 0 Gives many points
255, 0, 0 Takes away points
  1. Create an anchored part named PointPart with an attached script named PointScript.
  2. In PointScript, store the parent part in a variable.
  1. Set up the RGB value for blue, which will give players the least points.
Create a new variable named blue and set it to Color3.fromRGB(). To create a color, in the parenthesis, type 0, 0, 255, the RGB value for blue.
  1. Next, set up the green value (0, 255, 0) and the red value (255, 0, 0).
  1. Code variables for small and large amounts of points and a third for removing points.

Set Up the Script

Add the Players Service

To award points, you'll start working not just with the player's character object in the workspace which holds the humanoid, but also with player information which is stored under Players. The Player object found there will be named after the player and holds information like leaderboard stats.

To get to the player's information, add Players service to your script. Services are additional pre-built functions made by Roblox engineers to save you time. The Players service has a lot of helpful function for working with player information, like points.
  • Type:
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

The part will need two functions. One function to give players points based on the current color of the part, the other to listen if a player touches the part.

  1. On the next line, create a new function named givePoints() with a print statement for testing inside.
  1. Under that, create a second function named partTouched() with a parameter named otherPart.
  1. To find the name of the player that touched the part type:
  1. If a player touched the part, it’ll be stored inside the player variable. If not, then the variable will be empty. To award points, check if there is a player stored inside the player variable and if so, run givePoints().
  1. Connect the function to the pointPart's Touched event.
  1. Run the game and touch the part; you should see your test 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.