Properties and BrickColor
Properties control how objects look and function. Some properties an object might have are material, color, or shape. You can use code to change these properties and make things happen in-game. For example, by changing the color property of an object you can create a flashing disco floor.
Teaching Exercise - Introducing Properties »
Take any available object and have your class describe at least three properties of that object. Emphasize a mix of properties that are visual and functional.
- Color: yellow
- Edible: False
- Makes marks: Yes
- Line color: grey
The Properties Window
Many of an object’s properties are shown in the Properties window. To get an idea of some properties already being used by objects in your game:
- Select a part.
- Scroll through the Properties Window on the bottom right.
Can't see the Properties Window?
If you don't see the Properties window, click the **View** tab and then click the Properties button.
The script will use code to change the color of a part at the start of the game.
Set up the Part and Script
- Select an existing part or create a new one.
- Rename the part. This example uses PracticePart.
- Rename the script ChangeBrickColor.
- Delete Hello World.
You should always start new scripts off with a comment about what the script does. Comments are special lines that help coders remember what scripts are for but doesn’t actually run.
--and a note about what this script does. The text should turn green to let you know it’s a comment.
Locate the Part
To make changes to any part using code, you first need to tell the script how to locate the part you want to work with. Use the Explorer to figure out where a part is. Notice that PracticePart is under Workspace.
Now that you know PracticePart is under Workspace, turn this information into code that the program can understand.
gamebelow your comment.
- Use dots to separate words. On the same line, type
Roblox will autocomplete words as you type to help speed up the coding process. When the words appear, you can use the arrow keys to move down the list. Pick an option by pressing Enter.
Check Your Code Before Moving on
Make sure your code looks exactly like the code above, and that PracticePart is spelled and capitalized exactly like in the Explorer.
Changing a Property with Code
Almost there! Now, you’ll change the part’s color with the
To start changing the color:
.BrickColorafter the name of your part.
Teaching Tip - Checking in Stages »
It's important for new students to check their code every few steps to reduce the possibility of errors. Additionally, encourage students to identify and solve their own errors (or ask their peers). This will help students become more independent while troubleshooting.
When checking their work, ask students the following:
- Is the capitalization correct?
- Is everything spelled correctly?
- Are there any extra spaces?
Using RGB Values
To change the
BrickColor property, you’re going to create a new BrickColor that will replace the current one. It’s not like mixing paints though, programs use RGB values, the combination of red, green, and blue to create all the colors on your screen.
There are some rules for using RGB values:
- Use 3 decimal numbers; one for each color.
- Separate each number with a comma.
- Use numbers between 0 and 1. 0 means a color is all the way off. 1 means the color is all the way on.
Below are some examples of RGB values:
Create a new RGB Color
You’ll now use the
= sign to set a new color to replace the original part color. You can use the decimal numbers for the colors in the table above, or come up with your own.
Inside the parenthesis, add 3 decimal numbers (between 0 and 1), separated by commas.
Run Studio; your part should change colors.
Troubleshooting Your Code
Remember that errors are a natural part of the coding process. Try the following if you have any errors (like a red line in the Script Editor or error message in the Output Window).
Issue: Part is still gray or doesn’t change color as intended
- Make sure you followed all three rules for RGB values (number is 0-1, is a decimal, all numbers separated by commas).
- If you are doing random numbers, you may get a surprise color.
- Double check that the capitalization and spelling is the same as the code example.
Brickcolorwill not work, while
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.