Just like people speak different languages, so do programs! Meet Lua, the language spoken by Roblox and the perfect introduction to coding for kids, and specifically young gamers.
As a coding language, Lua is great for kids and teens who want to pick up a language quickly, are eager to make the popular game their own, or are just interested in seeing how the worlds of gaming and coding collide.
By learning Lua, kids will not only be able to build their coding repertoire (or even try it for the first time), they’ll delve deeper into game development.
Budding entrepreneurs might even get to make their own Robux along the way!
What is Lua coding?
Originating from Brazilian programmers, Lua (fun fact: this translates to "moon" in Portuguese!) is a straightforward programming language that’s designed to enhance applications. Translation: Lua is a useful language for customizing apps and games.
Currently, Lua is implemented by companies like Adobe and Firefox, and it’s the language that runs awesome games like those on Roblox and World of Warcraft. Impressive, right? But don’t be intimidated: like all the best coding languages for kids, Lua’s designed to be simple to learn with plenty of room to grow!
How to get started with Lua coding
With Roblox coding, kids can create anything they can imagine. Explosions? Check! New characters? Yep. Swords of awesome power? Absolutely! So, let’s take a look at a few beginner-level Lua coding projects.
(First things first, kids should download Roblox Studio, a free creative engine as limitless as your child’s imagination. Then, they can give one of these introductory Lua challenges a try.)
Making a New Script in Lua
Scripts contain pieces of code used inside Roblox, and can be as simple as a single line of code, or as long as a chapter book. When you create your first script, it will open the Script Editor automatically.
To create your first script, in the explorer:
- Hover over ServerScriptService to see the + symbol.
- Click the + symbol.
- Select Script from the drop-down.
- Right-click > Rename the script.
- Name the script "PracticeScript."
As a game designer, kids can expect to be creating a lot of scripts. So, it's really important for them to remember to name each script so they don’t mix them up.
To run the code:
- Press the Play button — the same one used to run the game.
Wait, what happened?
- Check the bottom of the screen; you should see the output window.
- Look closely and there will be a single line that says “Hello World.”
- If you don't see the output screen with the Hello World! text, click to the View tab at the top and select Output.
Want a different message? The "Hello World" message can be easily turned into anything young coders can think of. Talk about their favorite Roblox game, Dragons, Dragons in Roblox, Roblox in Dragons, anything!
Try creating a different message. In the Script Editor:
- Change the text that's between the quotation marks.
- Run the code.
If a mistake is made as one is coding, error messages will show up as red lines in the Script Editor and in the output window. To check code:
- Click in the Script Editor.
- Delete a quotation mark from the print statement.
- Hover over the red line to see the error message.
- The error message says “Expected identifier when parsing expression, got malformed string.” That usually means it’s missing a quotation mark.
- Put the quotation mark back where it was deleted from to resolve the error.
Ready to keep going? Here are a few simple Lua projects that will get kids closer to being a developer in no time!
Changing Brick Color with Code
When designing, kids will learn how to edit properties, such as the size and scale of certain parts while inside the editor. But they can also edit properties inside a script as well!
For example, they can use code to change a game's BrickColor property of a Part. To do so, they’ll need to do things like give the program directions on how to get to the Part of which they want to change the color. Then, they’ll have to learn how to replace a string variable in the BrickColor property, and more.
Another example of how coding can take things to the next level is the fact that a brick doesn’t have to just remain one solid color of red or blue! With the help of variables as mentioned above, and loops, Parts can more resemble a unicorn if anything, with colors that continually change forever.
To accomplish, kids are going to use code to change the BrickColor property of a Part when the game starts. This can be an existing Part in a game, or they can create a Part to practice with.
To set up:
Select an existing Part or create a new one.
- Rename the Part “PracticePart.”
- Add a new script to the PracticePart.
- Rename the script “ChangeBrickColor.”
Creating Trap Parts
Trap Parts harm or destroy anything that comes in contact with them, and thus should be avoided! Traps can be used to create new challenges, and can be implemented with the OnTouch function.
Again, this triggers whenever anything touches the Part, but can be taken a step further, and coded to, say, check that whatever touched the Part is human.
- Create a new Part and name it “Trap.”
- Insert a new script into the Part and name it “TrapScript.”
- Create a variable that stores access to the Trap Part: local part = game.Workspace.Trap
The goal is for the Trap to be triggered when a player touches the Trap Part, and Roblox has a function for just that called the onTouch function. This function triggers whenever anything touches the Part. Young creators can then test to see if the thing touching it is a human; if it is, then they'll destroy them!
- On a new line, write: local function onTouch(otherPart)
- Press Enter, and you’ll see two new lines and the word end .
All the function’s code will need to be between the line you just wrote and end .
To test if the thing that touched the Part is a human, the parent of the thing that touched the Trap must first be found. The name of the object that touched the Trap is otherPart .
- Write a variable: local character
- Store the parent of otherPart in that variable: local character = otherPart.Parent
Now that the parent of the thing that touched the Trap can be accessed, the next step is to check to see if the child is a humanoid. Roblox has a function called “FindFirstChildWhichIsA" which will be used to see if they’re a humanoid.
- Write another variable on a new line: local humanoid
- Call that function on the parent of the thing that touched the Trap: local humanoid = character:FindFirstChildWhichIsA(“Humanoid”)
If the thing that touched the Trap is a humanoid then the variable humanoid will be true; if they’re not a humanoid, the variable will be false.
- Write an if statement: if humanoid then
- Set their health to 0: humanoid.Health = 0
The last thing that needs to be done is to tell the function to activate whenever something touches it. This line of code will go at the very bottom of the script; it needs to go after both ends.
Get stuck? Try one of these troubleshooting techniques:
-Include a print statement within the custom function. If the print statement doesn't run when something touches the Part, the problem comes before that line of code.
-The statement connecting a function to the Touched event should come after the custom function.
-Make sure "Touched" is capitalized.
-Make sure the Touched event is being connected to the custom local function and not to the parameter.
Discover New Worlds with Lua and Roblox
Even if your child isn’t looking to monetize their creations (right away at least!), it’s still worth exploring the interdisciplinary potential of this versatile coding language. At the very least, kids can engage more meaningfully with the game while love learning alongside it—and if you ask us, that’s always worth doing!
To help get your child started on this path, iD Tech offers virtual after school learning experiences, 1-on-1 Roblox tutoring lessons, and summer camps that will help transform your child’s love of gaming into valuable skills.