Hello! I am xAstralMars, working on a simulator game. I started playing Lua Learning to teach people how to code, and to learn some new concepts myself. I think this game is very well done, and boatbomber really deserves some praise. Today we will learn about variables, the output window, math, and functions.
Luau is the programming language used by Roblox developers to create their games. If you have worked with other programming languages before, you will know how Lua is different than others.
The output window is the window shown when something is run, for example if you print a message, it will show the message in the output window. To try this, enter this code into the command line at the bottom of your screen.
print("Message") -- you can put any message here
In the output window, you should see whatever message you put. Now we will use this knowledge for concepts later in this tutorial, and subsequent ones.
Variables are very important once you get more advanced in coding. They will help clean up your code, because you dont want your functions ending up like this:
script.Parent.Parent.Parent.Parent.MouseButton1Click:Connect(function() script.Parent.Parent.Parent.Parent.Parent.Parent.Visible = false end
With variables it looks like this:
local button = script.Parent.Parent.Parent.Parent local frame = script.Parent.Parent.Parent.Parent.Parent button.MouseButton1Click:Connect(function() frame.Visible = false end
It looks a lot cleaner, doesn't it? Variables are represented with "local". To create a variable, put this in your script:
local var = "anything here"
With variables, you can do a lot of things. You can print the variables, use them in functions, and set them to math problems. We will learn about those later. First, let's put this in our script:
local var = "anything here" print(var)
You can also change variables too:
local var = "anything here" print(var) var = "something again" print(var)
Math operations can be very useful. You can use them to do math problems, add people scores together, and more! To make a math problem, use these symbols
- + (addition) - - (subtraction) - * (multiplication) - / (division)
Let's make a simple math script.
local problem = 1 + 1 --addition local problem2 = 3 - 1 --subtraction local problem3 = 4 * 2 --multiplication local problem4 = 4 / 2 --division print(problem,problem2,problem3,problem4) -- when printing, you can print more than one message
Now, in the output, you should see 2, 2, 8, and 2.
Functions are a big part of scripting. You can use them to clean up your code, and the DRY rule (don't repeat yourself.) The DRY rule basically just means don't do something over and over again, just make a function that does that. This is without functions:
local number = 1 print(number) number = number + 1 print(number) number = number + 1 print(number) number = number + 1 print(number) number = number + 1
With functions, it looks a lot cleaner:
local number = 1 local function addNumber() print(number) number = number + 1 end addNumber() addNumber() addNumber() addNumber()
You can use functions for anything, including events, which we will cover next.
This brings me to the end of this tutorial. I hope you learned something new. Next tutorial, we will cover events, services, and GUIS. Thank you for reading this!