Coding annotations for LUAU or LUA, used in Roblox.
● Printing Command:
The printing command is used for printing something inside of the Studio Console, for
example:
print(“Hello World!”)
This is the default message Roblox prints every time you make or open a new script.
Pressing Play when this command is inside of an active script will have the message
“Hello World!” appear on the console.
The Print Command is useful for testing if a certain outcome of a command is working,
for example, if you type a script with 2 different outputs, you can put a print command
inside of each output, that way only one of the print commands will be executed and you
will be able to test which output was selected.
● Data Types
There are many data types, but for now we will be using 4 main types
Nil → This data type indicates a value that doesn’t exist, sometimes being used as a 0,
although not necessarily being the same as 0.
String → This data type indicates a text value, it is represented by being inside of 2
quotes, like this: “Hello!”
Number → This data type indicates a numerical value part of the Rational Numbers (ℚ)
Boolean → This data type is used to indicate 2 outcomes, it can be set to true or false,
like yes or no or 0 and 1.
● Variables
Variables are ways of representing a specific value, or part, or location, etc etc. They can
also be used for shortening codes. And they can be anything, pretty much. For example,
when inside Roblox. If you want to refer to the baseplate inside of the game pre-model,
you have to type out:
game.Workspace.Baseplate
→ When, using a variable, you can shorten that by saying:
local baseplate = game.Workspace.Baseplate
→ Now the variable “baseplate” is referring to game.Workspace.Baseplate making it
much shorter, and more efficient.
This same technique can be applied to mostly anything in the game, for example if you
were to create a part named “Wall1”, you could type this in your script to shorten your
code every time you want to refer to this part:
local Wall1 = game.Workspace.Wall1
That way, you don’t have to type game.Workspace.Wall1 every time you want to refer
to Wall1.
● Properties
When going into Roblox Studio, you might notice a “Properties” tab whenever you select
something, these properties can be manually set by the player in the Studio, but they can
also be set within the game, inside of the script, for example, to change a part’s material:
1. local part = game.Workspace.Part
2.
3. local function changeMaterial()
4. part.Material = Wood
5. end
6.
7. changeMaterial()
Following this code, in the first line we shorten the code by creating a variable part to
refer to game.Workspace.Part, then we create a function, inside this function it sets the
part’s material to “Wood”, then, on the 7th line, the function is called by the script,
making it be executed.
The same applies to mostly all properties inside of the properties tab, just pay attention to
the values, some properties use boolean values, like castShadow where it can only be one
of the 2 values, true or false.
● Functions
Functions are, by definition, code blocks that execute a certain amount of scripts
whenever it is called, to create a function, we use this code:
local function <function_name>()
Example: local function castShadow()
Now that we’ve created our function, we can add a functionality to it inside of our code,
like this:
1. local part = game.Workspace.Part
2.
3. local function castShadow()
4. part.CastShadow = false
5. end
Now we have a function that sets our part’s CastShadow property to false, but for it to be
applied, we need to call it inside of the script. So, we type the function at the place we
want it to be executed, that way:
1. local part = game.Workspace.Part
2.
3. local function castShadow()
4. part.CastShadow = false
5. end
6.
7. castShadow()
Now, on the 7th line, I’ve called out my function, therefore, when the script is being read
by Roblox, it’ll understand it needs to execute the function castShadow(), it will read
what is inside of the function and execute it.