Roblox coding for beginners - Part 2

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.

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

3 Now insert a script INSIDE THE MODEL. Again, right click on model and select "script"

No alt text provided for this image

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)

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.

No alt text provided for this image

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!

To view or add a comment, sign in

More articles by Costanza Casullo

  • Cheating before Chat GPT

    Disclaimer: the purpose of this article is not blaming the Kenyan essay writers you will read about. I am writing it to…

  • The Business of Metaversities - Part 2

    Before you google “metaversities”, in this series of articles, the term is intended as the digital twin of a university…

    4 Comments
  • The Business of Metaversities - Part 1

    Before you google “metaversities”, in this series of articles, the term is intended as the digital twin of a university…

    2 Comments
  • Violent video games = violent kids?

    P.S.

    5 Comments
  • When real life fails you, the metaverse saves you

    Disclaimer: terms such as “autism”, “children affected by autism” or “on the spectrum” are going to be used in this…

  • Can kids code their way out of poverty?

    Before I attempt to answer the question Can kids code their way out of poverty? I acknowledge the fact that “coding”…

    3 Comments
  • Pocket-sized metaversic learning

    I like making up metaverse related terms lately… kids learning in the metaverse are “metaversians”, something relating…

    3 Comments
  • On the nature of technology

    According to dictionary.com, nature is the natural world as it exists without human beings or civilization.

    7 Comments
  • A poetic verse on all things meta

    Whether we like it or not, the COVID-19 pandemic is not over, climate change is threatening our existence, nobody has…

    2 Comments
  • Phoneraiser for online learning

    You’re at a restaurant. A family is sitting next to you.

    2 Comments

Others also viewed

Explore content categories