Roblox coding for beginners - Part 2
This is the second part of a series of tutorials for beginners getting started with Roblox coding. In part 1 I explained how to use code to change the properties of bricks in a game. The properties I used as an example were transparency and colour. So basically how to change the look of the bricks. This process is known as referencing. If you are a complete beginner, I suggest you go back to part because otherwise this second article might not make sense to you.
Now let's learn about variables.
1 Insert two parts in your game. Then select them both in the workspace, right click and group them.
2 You will now have a Model, which is made of those two parts. Rename the parts as part1 and part2. You have to right click on each part in the workspace and select "Rename.
3 Now insert a script INSIDE THE MODEL. Again, right click on model and select "script"
We are ready to code a variable
Now let's say we want part1 to be red and part2 to be blue. We learnt how to do that in the first tutorial
In the script we would put the code below, because we follow the hierarchy we use to access the properties of a brick to change its look:
game.Workspace.Model.part1.BrickColor = BrickColor.new("Really red")
game.Workspace.Model.part2.BrickColor = BrickColor.new("Bright blue")
However, this is just a simple example. Let's say you have 5 parts and you want to achieve the following:
1 A red part
2 An anchored part (which basically means it does not move around when the player touches it)
Recommended by LinkedIn
3 A transparent invisible part
4 A part with grass material
5 A part shaped like a ball
This is what the code would look like without variables:
game.Workspace.Model.part1.BrickColor = BrickColor.new("Really red")
game.Workspace.Model.part2.Anchored = true
game.Workspace.Model.part3.Transparency = 1
game.Workspace.Model.part4.Material = Enum.Material.Grass
game.Workspace.Model.part5.Shape = Enum.PartType.Ball
We need to make this more readable by using variables. Variables store information for us. Meaning that we will not need to refer to part 1,2,3,4 or 5 using the "long way" (game.workspace.Model.part1.BrickColor = BrickColor.new("Really red") ....). We will just write part1 followed by the property we want to change.
Here is how we do it. We need to make the computer remember that each part is located IN THE GAME, INSIDE THE WORKSPACE, INSIDE THE MODEL. To do this, we write local, which stands for variable and then tell the computer where each part is.
local part1 = game.Workspace.Model.part1
local part2 = game.Workspace.Model.part2
local part3 = game.Workspace.Model.part3
local part4 = game.Workspace.Model.part4
local part5 = game.Workspace.Model.part5
Now we have told the computer where each part is. We can modify the properties of each part without telling him that again.
local part1 = game.Workspace.Model.part1
local part2 = game.Workspace.Model.part2
local part3 = game.Workspace.Model.part3
local part4 = game.Workspace.Model.part4
local part5 = game.Workspace.Model.part5
part1.BrickColor = BrickColor.new("Really red")
part2.Anchored = true
part3.Transparency = 1
part4.Material = Enum.Material.Grass
part5.Shape = Enum.PartType.Ball
Variables store information for us and this information can be re-used any time in the code without having to re-write everything again.
Let's take a look at the result of all this. Notice part1 is now red. part2 does not move when the player touches it. part3 is invisible because we changed its transparency. part4 is made of grass ad part5 is shaped like a ball.
I do understand you might not see the point in implementing variables right now. You are probably thinking why can't you just copy and paste code anyway. It's true. Right now you can do that. However, you are going to need variables to implement functions, which will be the topic of my third tutorial.
That's it for today. It might not seem like you learnt a lot but you are going to thank me when you will create more complex games. The basics are everything, just like in any other learning domain.
Do you have any questions regarding Roblox coding? Send me a message on LinkedIn!