mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-24 06:34:09 +00:00
rules
This commit is contained in:
parent
a9b4f8e83b
commit
e6efc04756
8494 changed files with 495610 additions and 0 deletions
|
|
@ -0,0 +1,48 @@
|
|||
---
|
||||
title: Coding fundamentals
|
||||
description: Teaches the basics of coding with Luau.
|
||||
next: /tutorials/fundamentals/coding-1/landing
|
||||
---
|
||||
|
||||
### Series description
|
||||
|
||||
Start coding on Roblox with this easy-to-follow series covering the fundamentals of how to use the programming language Luau. These courses are perfect for those new to coding or just starting with Luau. Each course centers around a foundational computer science principle, and features individual lessons with step-by-step tutorials you can use to create your own experiences on Roblox.
|
||||
|
||||
Before beginning this course, the reader should have basic knowledge of Roblox Studio, as demonstrated in the [Studio](../../curriculums/studio/index.md) and [Building](../../curriculums/building/index.md) lesson.
|
||||
|
||||
### Series contents
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Series</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>[Variables and objects](landing.md)</td>
|
||||
<td> Create your first script and start making things happen with code. These tutorials cover variables, data types, and properties.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[Functions and events](../coding-2/functions-and-events.md)</td>
|
||||
<td>Use functions to create chunks of code that run only when you tell it. Connect your code to in-world events to create interactive objects like traps or bridges.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[Conditionals](../coding-3/landing.md)</td>
|
||||
<td>Conditionals use the coding pattern of "if this happens then do that." Apply this pattern to check if the time is right to unlock power ups for your players.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[Loops](../coding-4/landing.md)</td>
|
||||
<td>Learn how to repeat code a certain amount of time or make it run forever using for and while loops. Practice using these loop types to code timed bridges and flashing lights.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[Dictionaries and arrays](../coding-5/landing.md)</td>
|
||||
<td>Learn how to store a lot of data in a single variable using tables and arrays. Data structures such as these can store large data sets like a player's inventory.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>[Organize code](../coding-6/landing.md)</td>
|
||||
<td>Take your code organization a step further and learn about module scripts, a unique type of script that can interact with other scripts in Luau.</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
@ -0,0 +1,172 @@
|
|||
---
|
||||
title: Create a script
|
||||
description: Learn how to create a script in Roblox Studio using Luau. Great for beginners interested in coding or computer science on Roblox.
|
||||
next: /tutorials/fundamentals/coding-1/object-properties
|
||||
prev: /tutorials/fundamentals/coding-1/landing
|
||||
---
|
||||
|
||||
**Coding** is the process of creating instructions for computers to follow. Just like people use different languages, such as English and Spanish, so do programs. Roblox uses the coding language **Luau**.
|
||||
|
||||
This article will cover how to start coding in Roblox, introducing common concepts like scripts, data types, and variables. By the end, you'll be able to type out code that displays messages in Roblox Studio.
|
||||
|
||||
## Code with scripts
|
||||
|
||||
In Roblox Studio, lines of Luau code are held in **scripts**. These scripts give the experience sets of instructions on how to give players health points, create a rain of fireballs, or anything else imaginable.
|
||||
|
||||
### Create a script
|
||||
|
||||
Scripts are commonly created in **ServerScriptService**, a special folder made just for holding scripts.
|
||||
|
||||
1. In Explorer, hover over **ServerScriptService** to see the **+** button.
|
||||
<img src="../../../assets/education/coding-1/hover-over-serverscriptservice_400x400.png" width="40%" />
|
||||
|
||||
2. Click the **+** button and select **Script**. A new script will be created and the script editor will open.
|
||||
|
||||
3. Right-click Script and select **Rename**. Name the script _PracticeScript_. Naming scripts helps you and any teammates remember what each script does.
|
||||
|
||||
### Hello world
|
||||
|
||||
New scripts include a print function at the top of the script editor. **Print functions** display text on the screen. It's one of the first functions many people learn, and you'll use it often. This code will make "Hello world!" appear on the screen.
|
||||
|
||||
```lua title = 'Default code'
|
||||
print("Hello world!")
|
||||
```
|
||||
|
||||
To find the script next time you open up Roblox Studio, click on the name of the script above the 3D viewport, or double-click the script's name in Explorer.
|
||||
|
||||
### Test output
|
||||
|
||||
You can see the result of running the default code with the **Output** window. If you've never used it before, you'll need to enable it.
|
||||
|
||||
1. From Studio's Window menu, open the [Output](../../../studio/output.md) window.
|
||||
2. To test the script, [initiate a playtest](../../../studio/testing-modes.md#playtesting). `Hello world!` will show up in the output.
|
||||
|
||||
<img src="../../../assets/education/coding-1/helloworld-output_800x150.png" width="80%"/>
|
||||
|
||||
3. Click **Stop** to end the playtest. You can now return to the **Script** tab.
|
||||
|
||||
## Identify data types
|
||||
|
||||
Coding languages classify different kinds of values into **data types**. For example, one data type is a **number**. Number data types are self-explanatory as they are made up of only numbers.
|
||||
|
||||
Another data type is a **string**. Strings can hold numbers, letters, and characters. Take another look at the default code in the new script; the words and quotations within the parenthesis is an example of a **string** data type.
|
||||
|
||||
```lua title = 'Default code'
|
||||
print("Hello world!")
|
||||
```
|
||||
|
||||
Strings like `"Hello World"` always sit inside quotation marks, `"like this"`. More examples of strings are below. Notice how they hold a mix of letters and numbers.
|
||||
|
||||
- `"You just joined the game!"`
|
||||
- `"There are 50 players left"`
|
||||
- `"10"`
|
||||
|
||||
## Create variables
|
||||
|
||||
**Variables** are containers for information the program can use and change, like player names or points.
|
||||
|
||||
**Declaring** a variable is what coders call the process of creating a new variable. In Luau, to declare a new variable, type `local`, then type the name for the new variable. A variable that can hold a player name might look like: `local playerName`
|
||||
|
||||
<Alert severity="info">
|
||||
When declaring new variables, some coding languages require that you also state what data type the variable can use. For example, a variable in Java would be `String name = "Pavel"`. Luau only requires a name.
|
||||
</Alert>
|
||||
|
||||
In Luau, variables can be global or local. You'll usually use **local** variables. Local variables can only be used within the script or chunk of code where they were created. Global variables can potentially be used by other scripts, but too many global variables can make your experience slow and unresponsive. It's better to stay in the habit of making variables local unless necessary.
|
||||
|
||||
### Use variables and strings together
|
||||
|
||||
Time to declare your own variables. These steps will use a string to store the name of your favorite animal.
|
||||
|
||||
1. Delete `print("Hello world!")`. It's best practice not to leave unnecessary code in your scripts.
|
||||
|
||||
2. Declare a new variable by first typing `local`, then naming the variable `myAnimal`.
|
||||
|
||||
```lua title = 'Declares a new variable'
|
||||
local myAnimal
|
||||
```
|
||||
|
||||
<Alert severity='warning'>
|
||||
Variable names can't include spaces. Be careful not to include spaces or the code won't work as intended.
|
||||
</Alert>
|
||||
|
||||
### Name variables
|
||||
|
||||
Variables can be named anything, but good names will always describe their purpose. Generic names make your code difficult to read and update later. Coders will also use different capitalization styles to remind themselves how the variable is used within the script. A good default style is **camelCase**.
|
||||
|
||||
To write in camelCase:
|
||||
|
||||
- Begin with a lowercase letter
|
||||
- Leave out spaces
|
||||
- Capitalize additional words
|
||||
|
||||
Good Variable Names
|
||||
|
||||
- `playerPoints`
|
||||
- `numberStorageArray`
|
||||
|
||||
Bad Variable Names
|
||||
|
||||
- `myVariable` - Doesn't describe the purpose of the variable
|
||||
- `player name` - The included space will cause issues
|
||||
|
||||
### Assign values to variables
|
||||
|
||||
New variables are empty. To **assign** it a value, or put something inside its container, use the `=` symbol. In this case, assign the variable the name of your favorite animal.
|
||||
|
||||
1. After the variable name, type `=`.
|
||||
|
||||
```lua title = 'Equal assigns values'
|
||||
local myAnimal =
|
||||
```
|
||||
|
||||
2. On the same line, type a string that contains your favorite animal. Remember the quotation marks.
|
||||
|
||||
```lua title = 'Assigns "Porcupines" to myAnimal'
|
||||
local myAnimal = "Porcupines"
|
||||
```
|
||||
|
||||
### Use print() for your own messages
|
||||
|
||||
Print functions display text on the screen, as you saw earlier. It's one of the first functions many people learn since it's a simple way of giving the script a command. To see your variable, use the `Global.LuaGlobals.print()` function.
|
||||
|
||||
1. On a new line, type `print()`.
|
||||
|
||||
```lua title = 'Adds empty print()'
|
||||
local myAnimal = "Porcupines"
|
||||
print()
|
||||
```
|
||||
|
||||
2. Type the name of your variable within the parenthesis.
|
||||
|
||||
```lua title = 'Outputs "Porcupines"'
|
||||
local myAnimal = "Porcupines"
|
||||
print(myAnimal)
|
||||
```
|
||||
|
||||
<Alert severity="info">
|
||||
If you see a red error in the output editor, it's like there's an error in the code. Each error has a unique way of describing itself. Try and use the information from the error to fix your code.
|
||||
</Alert>
|
||||
|
||||
3. Test your code with the play button. You should see the name of your animal in the Output window.
|
||||
|
||||
### Combine strings
|
||||
|
||||
You can display any string in the Output using `print()`; you can even print multiple strings stored within variables or typed directly within the function. **Concatenation** is combining strings. To concatenate the string assigned to your variable and a second string, use two dots `..` The following example concatenates two variables and two typed strings.
|
||||
|
||||
```lua title = 'Uses variables and strings together'
|
||||
|
||||
local firstAnimal = "porcupines"
|
||||
local secondAnimal = "dolphins"
|
||||
|
||||
print("I like " .. firstAnimal .. " and " .. secondAnimal)
|
||||
```
|
||||
|
||||
Play around with printing different combinations of strings.
|
||||
|
||||
## Summary
|
||||
|
||||
New scripts can be created by clicking the + button next to the name of an object. ServerScriptService is a common place to create new scripts. New scripts include the default code `print("Hello world!")`. This code will display `Hello world!` in the Output window, where you can see the results of your code and if any errors have occurred.
|
||||
|
||||
"Hello world!" is an example of a string data type. Strings can include any combination of characters that you might type on your keyboard. Concatenation is when multiple strings are combined.
|
||||
|
||||
Variables are containers that can be assigned to hold different data types, such as strings and numbers.
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
---
|
||||
title: Variables and objects
|
||||
description: Learn how to start coding in Roblox Studio in this tutorial series.
|
||||
next: /tutorials/fundamentals/coding-1/create-a-script
|
||||
prev: /tutorials/fundamentals/coding-1/coding-fundamentals
|
||||
---
|
||||
|
||||
### Series description
|
||||
|
||||
For anyone interested in learning computer science or coding, this serves as a foundational series. This series is recommended for anyone interested in taking other coding lessons in Roblox Studio.
|
||||
|
||||
### Objectives and prerequisites
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td width="20%"><b>Learning objectives</b></td>
|
||||
<td>
|
||||
Understand the process of scripting, such as creating scripts and being able to identify interface elements like the Output window.
|
||||
|
||||
Utilize data types, and variables use strings to print messages to the Output window and change object properties.
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>Prerequisites</b></td>
|
||||
<td>
|
||||
Basic knowledge of how to create and manipulate parts in Roblox Studio.
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
### Series contents
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Article</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td><a href="create-a-script.md">Create a script</a></td>
|
||||
<td>Learn how to create new script objects and print messages to the Output window.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="object-properties.md">Object properties</a></td>
|
||||
<td>Change the color of a part using just code</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><a href="parents-and-children.md">Parents and children</a></td>
|
||||
<td>Instead of calling out a specific part name, learn how to navigate the hierarchy to make code that will work for any part</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
---
|
||||
title: Object properties
|
||||
description: Learn how to use Luau to change the appearance properties of different objects, such as color and shape.
|
||||
next: /tutorials/fundamentals/coding-1/parents-and-children
|
||||
prev: /tutorials/fundamentals/coding-1/create-a-script
|
||||
---
|
||||
|
||||
**Properties** control how objects look and function. Each object in Roblox Studio has its own set of properties. For example, a part object has color, size, and shape properties. Properties can be changed in the Properties window or through code.
|
||||
|
||||
To learn about properties, you'll explore common properties found in parts and then write a script to change part colors.
|
||||
|
||||
## The Properties window
|
||||
|
||||
The **Properties** window can be used to learn about an object's properties. Use it to take a look at a part's properties.
|
||||
|
||||
1. Select a part.
|
||||
|
||||
2. In the **Properties** Window on the bottom-right, look at the different properties that can be changed, like color, size, material and transparency. You can also change most properties in this window from within a script.
|
||||
|
||||
<img src="../../../assets/education/coding-1/part-properties_400.png" width="40%" />
|
||||
|
||||
## Add comments to scripts
|
||||
|
||||
Comments are special lines starting with `--` that help coders remember what parts of scripts do. Unlike other code, comments don't run; they're just there so you can leave notes to yourself and other programmers. This script will change a part's `Class.BasePart.Color|Color` property at the start of the experience.
|
||||
|
||||
1. Select an existing part or create a new one. Rename the part PracticePart.
|
||||
|
||||
2. In **ServerScriptService**, create a new script named ChangeColor.
|
||||
|
||||
3. In the script, delete the default code. Then, write a **comment** by typing `--` and a brief description of what the script will do.
|
||||
|
||||
```lua title = 'Describes what the script does'
|
||||
-- Changes the color of a part
|
||||
```
|
||||
|
||||
### Locate the part
|
||||
|
||||
To make changes to a part, you must be able to describe the part's location. The Explorer is an excellent tool for **referencing** locations. In this case, **PracticePart** is under **Workspace**.
|
||||
|
||||
<img src="../../../assets/education/coding-1/practice-part_400.png" width="40%" />
|
||||
|
||||
Now that you know where the part is, the part's location needs to be translated into something a script can understand.
|
||||
|
||||
1. Under the comment, type `workspace.PracticePart`.
|
||||
|
||||
```lua title = 'References PracticePart in Workspace'
|
||||
-- Changes the color of a part
|
||||
workspace.PracticePart
|
||||
```
|
||||
|
||||
### Change a property with code
|
||||
|
||||
You'll use an RGB value to change the part's color. Computers use **RGB values**, the combination of red, green, and blue, to create all the colors on your screen.
|
||||
|
||||
RGB values use three numbers from 0 to 255, separated by commas. For example, black is `(0, 0, 0)` while white is `(255, 255, 255)`.
|
||||
|
||||
For the part, the script will change its Color property to a new Color3, a data type that stores colors.
|
||||
|
||||
1. After `PracticePart`, type `.Color` to access the Color property.
|
||||
|
||||
```lua title = 'Accesses the Color property'
|
||||
-- Changes the color of a part
|
||||
workspace.PracticePart.Color
|
||||
```
|
||||
|
||||
<Alert severity="info">
|
||||
Roblox will autocomplete words as you type to help speed up the coding process. You can use the arrow keys to move down the list when the words appear. Pick an option by pressing <kbd>Enter</kbd>.
|
||||
</Alert>
|
||||
|
||||
2. Next, type `= Color3.fromRGB()` This code will allow you to assign a new color.
|
||||
|
||||
```lua title = 'Uses Color3.fromRGB()'
|
||||
-- Changes the color of a part
|
||||
workspace.PracticePart.Color = Color3.fromRGB()
|
||||
```
|
||||
|
||||
3. RGB color values can be manually typed inside the parentheses, but using the color picker is easier. Click **inside** the parentheses, and then click the color wheel. Follow the popup to create a color.
|
||||
|
||||
<img src="../../../assets/education/coding-1/color-picker_800x140.png" width="80%" />
|
||||
|
||||
Your code should look similar to the code below.
|
||||
|
||||
```lua title="Updates the color of PracticePart"
|
||||
-- Changes the color of a part
|
||||
workspace.PracticePart.Color = Color3.fromRGB(255, 230, 50)
|
||||
```
|
||||
|
||||
<Alert severity="info">
|
||||
If you want to type an RGB value manually, make sure each number is between 0 to 255 and separated by commas, such as (10, 50, 10).
|
||||
</Alert>
|
||||
|
||||
4. [Initiate a playtest](../../../studio/testing-modes.md#playtesting) to test that your part changes color.
|
||||
|
||||
## Summary
|
||||
|
||||
All objects have properties. Parts have properties like color and transparency. At the same time, other object types have their unique properties.
|
||||
|
||||
To change the color of a part, you need to be able to describe where to find it. If the part is in Workspace, use the keyword `workspace`. Then use dot operators to state the desired part and access its properties.
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
---
|
||||
title: Parents and children
|
||||
description: Practice navigating object hierarchies instead of using specific names in your code.
|
||||
next: /tutorials/fundamentals/coding-2/functions-and-events
|
||||
prev: /tutorials/fundamentals/coding-1/object-properties
|
||||
---
|
||||
|
||||
Instead of running scripts from ServerScriptService, you may want to attach a script to the part. For instance, using a script to turn a part into a trap or make a part give the player points when they touch it. By understanding how scripts and parts are related in the hierarchy, you can create a script that works automatically without naming which part to use.
|
||||
|
||||
To do this, you must understand **parent and child relationships**.
|
||||
|
||||
Parents and children are ways to describe the hierarchy between different objects. Anytime you've added a new part to Workspace, Workspace has been the parent object, and the part became a child object. When you added a script to ServerScriptService, ServerScriptService was the parent, and the script was a new child.
|
||||
|
||||
## Set up the project and script
|
||||
|
||||
To demonstrate parent and child relationships, you'll start with a single part that changes colors and then duplicate the script across multiple parts.
|
||||
|
||||
1. Create a new part and rename it. This lesson will use ColorPart.
|
||||
|
||||
2. Right-click the **part** and select **Insert Object** ⟩ **New Script**. Rename the script **ColorChanger**.
|
||||
<img src="../../../assets/education/coding-1/color-changer_400x216.png" width="40%" />
|
||||
|
||||
3. Copy and paste the code below into **ColorChanger**. This version of the code is the same that you used before. It assigns a specific part to a variable.
|
||||
|
||||
```lua title="Changes the color of ColorPart"
|
||||
local Workspace = game:GetService("Workspace")
|
||||
|
||||
local colorPart = Workspace.ColorPart
|
||||
colorPart.Color = Color3.fromRGB(50, 240, 255)
|
||||
```
|
||||
|
||||
4. Test and check that the part changes color as desired.
|
||||
|
||||
## Parent and child relationships
|
||||
|
||||
A **parent** is anything with objects, like scripts or parts, attached below it. Anything under the parent is its **children**. In the example below, ColorPart is the parent, and ColorChanger is the child.
|
||||
|
||||
<img src="../../../assets/education/coding-1/color-changer_400x216.png" width="40%" />
|
||||
|
||||
With the current script, you can only change the color of a **single part** named ColorPart. To change any part's color, you can design the code to work on the script's parent object, whatever it happens to be named. The code `script.Parent`, will go up the hierarchy and find the object the script is attached to.
|
||||
|
||||
### Use script.Parent
|
||||
|
||||
`script.Parent` can be assigned to a variable like any other value with the `=` symbol.
|
||||
|
||||
1. In the script, replace the named part with `script.Parent`. Check your code below.
|
||||
|
||||
```lua title="Changes the color of the parent object"
|
||||
local colorPart = script.Parent
|
||||
colorPart.Color = Color3.fromRGB(50, 240, 255)
|
||||
```
|
||||
|
||||
### Test reusable parts
|
||||
|
||||
Now that the variable will refer to whatever part the script is attached to, you can make as many copies as you want.
|
||||
|
||||
1. Right-click on the color part and select Duplicate, or use the hot-key <kbd>Ctrl</kbd><kbd>D</kbd> (<kbd>⌘</kbd><kbd>D</kbd>). Create at least three total parts.
|
||||
|
||||
2. Run the experience to check that all parts change color.
|
||||
<video controls src="../../../assets/education/coding-1/scriptParent_finalExample_web.mp4" width="80%"></video>
|
||||
|
||||
## Summary
|
||||
|
||||
Instead of always directly referencing parts by name and location, script.Parent can be used as an alternative. That code will grab the parent, or object, that the script is attached to, thus letting the script make modifications to it. Using code such as `script.Parent` will come in handy to make your code reusable. One drawback to remember is that you now have multiple scripts that require updating if you want to change the code.
|
||||
Loading…
Add table
Add a link
Reference in a new issue