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 »
Troubleshooting Tips »
- 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.