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

Creating a Point Giving Part

15 min

Creating a Point Giving Part

Person running towards color changing blocks to get points

Learning Objectives Students will be able to:
  • Use else and else-if to award players different amounts of points based on what color a part is when touched.
  • Practice good programming style by indenting their code to keep their if statement organized and easy to read.
Prerequisites Students should:

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.


  1. On your own, code a while true do loop that changes pointPart to the color variables you’ve created. Don’t forget to use wait() between colors.

  1. Playtest and check that all three colors loop without stopping.
  • Check that the while loop is at the bottom of the script, below the Touched event. If the loop is not at the bottom, it’ll keep other parts of the script from running correctly.
  • Check that each color inside Color3.fromRGB() is correctly written. There must be three numbers between 0 and 255 separated by commas, like (255, 50, 0).

Giving Players Points

Because each color gives a different amount of points, the script will use an if statement to check what color is active when touched and give points based on that color.

Find the Current Color

Before the player can be awarded the right amount of points, you need to set up variables to capture what color the part was when the player touched it and the amount of points the player already has.

  1. Find givePoints()
  2. Replace your testing message with a variable for the current color of pointPart.
  1. Next, add a variable for the player’s leaderboard.
  1. Now add a variable to get the player’s Points value, which is a child of their leaderboard.

Give or Subtract Points

Next, you'll use if and elseif to give or subtract points depending on the color of the part when touched.
smallPoints
largePoints
losePoints
  1. Inside givePoints(), beneath the variables, use an if statement to check if the current color is blue and if so then add smallPoints to the player’s current points value.
  1. To check for green, add an else if condition. If green, then add the largePoints variable to the player’s points.
  1. Use an else statement to subtract points if pointsPart was neither blue nor green.
  1. Lastly, destroy the part after the if statement so that the script can’t keep giving out points.
  1. Playtest and check that each color gives points as expected.
Testing Every Condition

When working with if statements with multiple conditions, it’s important to test that every elseif and else statement works. It’s possible to test one statement, think everything works, but then discover later on there’s a bug in one of the statements that could have been caught earlier.



Giving Players Feedback

The PointPart works, but players might not notice something happened unless they happen to be looking at their leaderboard. Fix that by creating particles when the PointPart is destroyed.

Adding feedback when players use a part, like sounds, shakes, or particles, makes interactions with objects more satisfying to players.

Create a Particle Effect

The particle effect will be the same color as the part when touched. Since the colors were stored in variables, it’s easy to reuse them.

  1. In givePoints() at the bottom, create a new ParticleEmitter instance. Make sure the instance name is spelled exactly as shown.
  1. ParticleEmitters use color sequences to control their Color property. Create a new ColorSequence and pass in the current part color.
  1. The particle will need to be parented to player that touched it. Create a variable to get the player’s Character model.
  1. Using the character, you can parent the particle to that player’s head.

Because scripts in Roblox run at different times, it’s possible that a player’s head might not be created yet. To avoid errors, like trying to find a part that doesn’t exist, the script uses WaitForChild() to get the head part attached to the player’s character instead of using the dot operator, like player.Head.


  1. Use wait() destroy the particles after one second.
  1. Playtest the game and make sure particles briefly follow the player after touching each color.
Troubleshooting Tips
  • Make when creating a new instance that ParticleEmitter is spelled exactly as shown and inside quotations.
  • When parenting the particles, make sure to use : between playerCharacter and WaitForChild() with no spaces between.

Finished Project Sample

Project File

Download the finished project here.

Finished Script


These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.