Getting Started with Lua in Roblox Studio



Have you ever wondered how people are able to make such complex games in Roblox? Part of it is the code, specifically in the coding language of Lua. Lua is an open source language built on top of C programming language. It is used for all sorts of applications. Here is a guide on getting started with Lua coding in Roblox Studio.

Code

Coding is basically typing out instructions that computers can understand. Code can be in different languages like Javascript, Python, and HTML. I compare these different computer coding languages to languages that people speak such as English, Spanish, German, or French. Lua is the coding language that is used for coding in Roblox.

Scripts

When you have Roblox studio pulled up, look at the explorer tab. Go to the “ServerScriptService” option in the explorer tab. Hover your mouse over it, and click the plus icon that appears. Then click the “Script” option. This will open a tab known as script editor. In the explorer tab, you can right click on a script and rename it to whatever you need to name it to. Naming scripts is a good way to keep scripts organized if you are going to end up having a lot of them. When you create a new script, be aware that the script editor will usually open up automatically without you having to open it.

Whenever you create a new script, you’ll see a single line of code in the script editor. The single line of code is the most often seen example of the print function. Print is one of the first functions people learn in coding classes, because of how simple the function is. With print, you just have to type: print(“Insert Text Here”) and when you run the commands the text will be printed. However the text in the “Insert Text Here” part can be replaced with whatever you want. Usually when the print function is first taught, it is taught in the form of: print(“Hello World”). You may have heard of “Hello World” before if you are familiar with code. “Hello World” is a phrase usually taught in the beginning of most coding classes. 

“Hello World” or “Insert Text Here” are both examples of a type of variable. Variables are containers that contain the information that the computer can understand and do numerous things with, depending on what the rest of the code says to do. Names and numbers are examples of variables, however there are many other possibilities in terms of what a variable could be. Many variable types exist, and “Hello World” and “Insert Text Here” are examples of string variables. String variables are used to store text so it can be brought up and used later on. String variables are always in between a pair of quotation marks. If no quotation marks are there, the computer may not register the text as a string variable, which could lead to issues or errors. Think about it like this. The program is trying to tell the computer something, hence the quotation marks. It is like the program is talking. Without the quotation marks, the computer gets confused and is unsure of what to do, and an error occurs.

If you want to change the text of the print script in Roblox studio, hover over “ServerScriptService” then go to the specific script, and replace the default “Hello World” with whatever you would like. Make sure the string of code is in parentheses, like in print(“Hello World”). Without the parentheses or quotation marks, an error could occur.

If you want to test the code, go to the view tab in studio, then click output, then go to the home or script menu tab, and press play. This will test the code, even if nothing happens in the main playtest window. What was created in the string of code, will be shown in the output tab, which is usually below the normal playtest window. If the code works how you wanted it to work, stop the playtest. If it did not work, go back to the script tab to make changes to try to fix the errors.

Debugging

Whether you are a new coder or an experienced one, you may need to debug your code once in a while. Debugging or troubleshooting is the act of looking through your code to find any pesky errors that need fixing. If your code did not work, make sure the string is surrounded by quotation marks, then parentheses, and so on. I suggest looking through your code a few times, because sometimes the errors are not easy to catch by skimming through the code. I have taken several coding courses, and sometimes I needed help with errors and debugging, and the code had to be looked through numerous times before the errors were caught. Sometimes we had to debug as a class to find everyone’s errors. 

Error On Purpose

If you are unsure what an error would look like in terms of testing, I suggest you should make an error on purpose. Do this by removing a quotation mark or parentheses from a line of code, or make a spelling error in a variable name on purpose. Or you could make a more complex error if you’d prefer. When a line of code has an error, it will be underlined in red. Hover with your mouse over the red underline to see what is causing the error, and then fix it accordingly. The output tab also shows errors so if you are unsure what is causing the error or how to fix it, check both the output tab and red underlines within the script tab.

Properties

Properties control things such as the appearance of an object, or what a specific object does, or if it does anything at all. For example some properties may give an object it’s color, or shape. To see the properties for a part that is already in the game’s world, select the part, then scroll through its properties inside the properties window. Be careful you do not miss the properties you are looking for. Before you work on code, you should remove the “Hello World” script, if you don’t want “Hello World” to occur in the code of that part. You remove code by highlighting it, then backspacing to delete the text.

Changing the Color of a Part

First you have to start the code with “game” without quotation marks. Then find the location of the specific part you want to change. In this case it is in workspace, and add that to the code after a period. Therefore the code would then be “game.Workspace”. Then you have to add the name of the part into the code. The phrase “.BrickColor” should be added so the computer knows to change the color of the part. Now we need to say what color it should change to. To do that we need to insert RGB (Red, Green, Blue) values into the code. You have to use 3 decimal numbers, one for each RGB value. Use numbers, or decimals. Numbers from 1 to 9 are different levels of a color being on or off. 0 would mean that a color is completely off. Decimals can also be used as part of the RGB color code for a part. For example brick color (0.4, 0.1, 0.9) is a nice shade of purple. To make a part change color you must add “= BrickColorNew” as well as the RGB color code of your choice in parentheses, to make the part change color. For example: game.Workspace.Part.BrickColor = BrickColor.new(0.4, 0.1, 0.9) turns the specified part purple. You should playtest to make sure the part changes color as it should. Be aware if you are doing random numbers in the RGB values, you might not get the color you originally expected from those RGB values.

Comments

Comments are little bits of text that say what certain lines of code do. Comments are just there for visible purposes and do not do anything. To make a comment, you should type — (2 dashes) and a note about what the code will do. The text should turn green, and if it does, then you have a comment. If you are using the dark theme in Roblox the comment will turn a shade of medium dark gray instead of green.

Looping Code

If you want a part to change to multiple different colors, such as with the case of making a dance floor, you’ll need to make a code loop. To do this, first we must create a local variable, which is a variable that only works in the script it was typed in. The local variable for looping is: local loopingBlock = game.Workspace.LoopingBlock. However, loopingBlock can be changed to the name of the part you are using. Be sure all times that your substitute of “loopingBlock” is in all the proper places, or you will get an error. Next, create the loop, specifically a “while true do” loop. It is often called just a while loop. In Lua, you create it by typing “while true do” on the next blank line in your script area, then enter, and a “end” word will be added for you a couple lines down. Between “while true do” and “end” add a line of code that changes the color of a part. The color it changes to does not matter. However, if you try to run the script now, it will cause an error due to the fact it will change colors so quickly, Roblox Studio will overload and be overwhelmed, therefore an error will occur. I suggest waiting on testing it until more code is added to the script. When code is “in scope” in means it is inside a specific command, or in this case, loop. If it is outside the command or loop, it will not run properly, so make sure you type your code in the right places, if you want it to work properly. To make the command wait a certain amount of time before doing the next part, in this case changing colors, you need to add a wait function. It is simple. It is: wait( ). With a number going into the parentheses for how long you want before it changes color. Then add a second color, so the part changes color between those two colors. Then add another wait function, so the second color doesn’t change too fast that you can’t see it change. If done correctly, when you playtest, the part will change between the specific colors you gave. You can add more than two colors though, there is no limit to how many colors you can have something change to. Just remember to add a wait function after each new color, so a color doesn’t change too fast.

Reusing Code

Instead of having to rename parts when you want to reuse code, there is an easier solution. First you need to change your code a little bit, to make it reusable. Do this by changing “local loopingBlock = game.Workspace.loopingBlock” to local “loopingBlock = script.Parent”. However, be sure your script is part of the specific part you want to change color. Do this by clicking the plus that appears in the explorer tab, near the name of the specific part, then chose the script option. If you already have code you need to edit typed in another script, just copy paste it from that script to the one for the specific part. A “Parent” is something that has things attached to it, in this case a script. The things that are attached to the “Parent” are like children. So a part would be a parent, while a script, or other thing attached to the part is its child. The script.Parent code tells the child to find its parent, then activate the code appropriately. If done correctly, you can easily copy paste the code to other parts, without having to change anything in the code, unless you need to change a part of the code for a separate reason, such as if you wanted to change the colors that the part changes to.

This screenshot here is a review of what was discussed in this article, in visible code form.

I hope this helped you get started with Lua in Roblox Studio. I’ll “see” you in our virtual art world again very soon!

Mikayla Finley

Hi I’m Mikayla! I’m primarily a digital artist, but I love all types of art projects. I promise to keep my project posts varied and interesting in our virtual art world. I hope you enjoy trying all of them.

Recent Content