- 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.