Roblox Lua Coding
All the objects inside of your game are listed in the Explorer. If you drop an object on another object, the dropped object will go underneath and to the right a bit. This makes the object above the parent, or the one that is in charge. Add two Parts and drop one on top.
You’ll see that if you move Part 1 that Part 2 moves as well. Part 2 is the Child, doing what the Parent says. Part 1 is the child of the Workspace. Anything with a “V” next to it is a parent.
Placing a Script
Delete the child Part. Then right click the remaining Part. Add an Object called a Script. It looks like a piece of paper. A screen with some code will pop up. The code says to display the words “Hello world!”
Press Play and see the phrase in the Output. This happened because the Part used the Script to say the words. You can change the words to whatever you want. Try it!
Variables are words or numbers that can store other words or numbers. You can change variables with your code. For example, a NumberOfSwords variable in a game can hold the number 2. Then when you sell it, NumberOfSwords is changed to one. Think of it kind of like the memory of your brain.
There are 3 main types of variables: Numbers, Strings, and Booleans.
Numbers are… numbers! Strings are words and must have these ” around them. Booleans is simply True and False. Make this code in your game.
Press play. What did it print out? Did it print the word spoons, word and fun? No, it printed what the variables stored!
Ok, so lets make a variable called swords and potions. Set swords =4 and potions = 3.
Then use the command:
print (“I have ” .. (swords + potions) .. ” items in my inventory.”)
Press Play and find out how many items you have. See how variables can be useful for things like inventory or counting coins or calculating health?
What is a Function?
Sometimes you will have complex activities that will require multiple lines of code. Rather than repeating lots of lines of code, you can store it all in a function. You type function then the name of it and then two parentheses.
Type this into a script, then run the game:
print(“Let’s have fun!”)
print(“Join the server.”)
Touch Part Function
There’s lots of times in Roblox that you have to touch a block to win or something can hurt you if you touch it. Delete anything you have in the game. Place a Part in and make it float in the air by checking anchored. Make a child script to the Part that does the following:
print(“Hey, you are touching me!”)
This is another reason to use functions. You can call them when certain things happen. “script.Parent.Touched” is a code that checks if the parent of an object has been touched = the floating part. “connect(brickTouch)” tells the code that if the Part has been touched, call the function brickTouch.
The Floor is Lava
Create a Part and name it Lava. Child a Script to the Lava.
Write this code in the Lava’s Script: