Creating a Point Giving Part
Creating a Point Giving Part
Students will be able to:
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.
- Create a new script in ServerScriptService named Leaderboard.
- Copy and paste the code below into the Leaderboard script.
Create the Color Changing Part
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
- Create a part named PointPart with an attached script named PointScript.
- In PointScript, use
script.Parentto refer to the part.
- Create a variable named blue with the matching RGB like below. This will give players the least points.
- On your own, set up the green value
(0, 255, 0)and the red value
(255, 0, 0).
Code Solution »
- Add variables for a small amount of points, a larger amount of points, and a third for removing points.
Add the Players Service
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.
- Create a new function named
givePoints()and a parameter named
player. Inside, add a print statement to use for testing.
- Under that, create a second function named
partTouched()with a parameter named
- Inside the function, use
GetPlayerFromCharacter(otherPart.Parent)to look for what player touched the part.
- If a player touched the part, it’ll be stored inside the
playervariable. If not, the variable will stay empty. On your own:
- Inside the function, check if
playerhas a value. If there is, then call
- Beneath the function, connect
- Playtest and check for your testing message.
Code Solution »
- Check that the
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.
- At the end of the script, create a new
while true doloop.
Why Put the Loop at the Bottom?
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.
- On your own, code a
while true doloop that changes pointPart to the color variables you’ve created. Don’t forget to use
- Playtest and check that all three colors loop without stopping.
Troubleshooting Tips »
- 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.
- Replace your testing message with a variable for the current color of pointPart.
- Next, add a variable for the player’s leaderboard.
- Now add a variable to get the player’s
Pointsvalue, which is a child of their leaderboard.
Give or Subtract Points
elseifto give or subtract points depending on the color of the part when touched.
- Inside givePoints(), beneath the variables, use an if statement to check if the current color is blue and if so then add
smallPointsto the player’s current points value.
- To check for green, add an
else ifcondition. If green, then add the
largePointsvariable to the player’s points.
- Use an
subtractpoints if pointsPart was neither blue nor green.
- Lastly, destroy the part after the if statement so that the script can’t keep giving out points.
- 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
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.
givePoints()at the bottom, create a new ParticleEmitter instance. Make sure the instance name is spelled exactly as shown.
- ParticleEmitters use color sequences to control their
Colorproperty. Create a new
ColorSequenceand pass in the current part color.
- The particle will need to be parented to player that touched it. Create a variable to get the player’s
- Using the character, you can parent the particle to that player’s head.
WaitForChild() Helps Avoid Run-time Errors »
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
wait()destroy the particles after one second.
- Playtest the game and make sure particles briefly follow the player after touching each color.
- Make when creating a new instance that ParticleEmitter is spelled exactly as shown and inside quotations.
- When parenting the particles, make sure to use
WaitForChild()with no spaces between.
Finished Project Sample
Download the finished project here.
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.