Begineer guide to scripting - Part 2

Begineer guide to scripting - Part 2

This is a post created to teach new upcoming scripters about roblox development. Go ahead and watch part 1 before reading part 2 as I will be using things taught in part 1.

Contents:

  1. Function Parameters
  2. Instancing
  3. Variables
  4. FindFirstChild
  5. Referencing

So let’s get into our first thing

Function parameters

You might be thinking what are function parameters?

Well, imagine that you created 50 parts using functions and you wanted one of those parts to be called “hello” and for it to not be anchored. Well, how do you do that?

How to make function parameters

Lets first create a function

function partcreate()
      local part = Instance.new("Part")
      part.Material = "Grass"
      part.Name = good
      part.Parent = workspace
end

partcreate()
partcreate()
partcreate()

There we go! We made a part, but let’s say that you want to make the first part not anchored, instead of true, and the name of the part hello, instead of good.

Well, how do we do that!
That is where function parameters come in!

function partcreate(name,material)

So you see we made the function’s name but what are those two words inside the brackets? Those are parameters. Now for the rest of the code we would write this

function partcreate(name,anchored)
      local part = Instance.new("Part")
      part.Anchored = anchored
      part.Name = name
      part.Parent = workspace
end

partcreate("hello",false)
partcreate()
partcreate()

This makes the first part called hello, and makes it unanchored as it is set to false. Now you can name the parameters anything you want, but try and keep it something related to the property your changing as it makes it easier to work with.

Another question is, why are the name property set to name, not “hello”?

Well, it’s because we want to set a parameter for the name property. That is why we put name for that line.

You also must put the name property inside speech marks as it only takes in strings.

Instancing

What is instancing?

As you can clearly see, I have been instancing a lot throughout my part 1 and 2. You might be thinking what is it? Well, instancing is basically adding a part or folder or something similar, to your game while it’s running.

So say deathrun, you will need to keep added parts to your game, that’s is where instancing comes in. It automatically makes a part into your game.

Using instancing

local part =  Instance.new("Part")

This made a part in the game, but you can’t see it. Why? Well we didn’t parent the part to any services or anything.

Lets parent it

local part = Instance.new("Part")
part.Parent = workspace

Now you can see the part, as now we parented it to workspace. You also have to put the item your creating in speech marks and also have capitalization.

You can also make something instead of a part if you need to that is.

local folder = Instance.new("Folder")

Just remember to parent it to something.

Bad practises

local part = Instance.new("Part",workspace)

This is a very bad practise. I recommend no developers use this.

Many people use this bad practise.

NEVER USE IT!

Common problem:

I can’t access the part I instanced.

Well that is usually because you didn’t make it as a variable. I will later be teaching what variables.

Variables

What are variables?

Variables are something that can hold value in it, it may be a boolean, string or number and many more.

Why are variables important?

Variables are important as you can use them to store values, and use them for later. This makes scripting much much easier for you.

How can we use variables?

We can write variables like this just remember to always have a local behind it.

local hello = 5

This literally means that the variable hello holds the value of 5.

Now say you wanted to show the number 5 in the output.

You would do this

print(hello)

Without the speech marks.

That is just a very basic first starting to variables, you can go check more about variables at the developer education website.

FindFirstChild

What is FindFirstChild?

FindFirstChild is a word in scripting, that finds the first child inside the service or whatever you say.

What is a child?

A child is a thing inside of another thing. Let’s for example say that there is a part inside of workspace. Part is the child of workspace.

Uses

Lets say there is a part called gravity.

Well if wanted to change the transparency of it, it will error.

game.Workspace.gravity.Transparency = 1

Why would it error?

Well gravity is a property inside of a part. Roblox thinks that you are thinking that there is a property inside of a property which can not be.

This is where FindFirstChild comes in.

game.Workspace:FindFirstChild("gravity").transparency = 1

This finds the first child named gravity inside of workspace and changes the transparency of it to 1.

That is basically what FindFirstChild is.

Referencing

Sometimes the name of your part is two words or a number or something similar. So you want to change the property of it.

game.workspace.56.Transparency = 1

Wait! ERROR! As you know the name property only takes strings in, and 56 is a number not a string.

So you do this

game.workspace["56"].Transparency = 1

You basically converted a number into a string.

This is same for two words.

game.workspace["hello brick"].Transparency = 1

That is all!

A part 3 might be coming soon!

EDIT: I’m making this edit to inform everyone that part 3 won’t be coming. I feel like this is enough for a begineer. Knowing isA() and tables, as a begineer is just out of what we call “begineer” more like “begineer-intermediate.” So, yeah. Hope you respect my descision.

7 Likes

Great tutorial! One thing I would recommend is more proper indenting, as it is very important in the readability and organization of code.

function partcreate(name,anchored)
local part = Instance.new("Part")
part.Anchored = anchored
part.Name = name
part.Parent = workspace

partcreate("hello",false)
partcreate()
partcreate()

Also in this block of code, there is no end statement for the function “partcreate”

1 Like

This is quite needed since there are many people who are trying to learn lua scripting, Nice job.

1 Like

You forgot the end

Shouldn’t this be FindFirstChild?

You didn’t indent anything here.

1 Like

Nice tutorial but it’s kinda hard to read without the formatting and headers.
Like how you even listed some bad practices so beginners don’t run into those.
:+1:


Some Issues:

game.Workspace is a fixed version of this.

And you could show them the documentation. Would be nice for them to explore the API also to see more code snippets/examples.

PS: Remind them that :FindFirstChild can be a pain sometimes according to the docs:

But beginners don’t need to stress themself out on performance/optimizations… yet.

Some Post Improvements:

Well, the DevForum usually uses markdown/HTML/raw text, this is super helpful for making a nice post/reply.

MarkDown:

  • Website to help you out: Here
  • You can even test it out: Here

HTML:

  • You don’t really need to use this/learn this
1 Like

Thank you! I will change these right away!

Actually didn’t know that FindFirstChild takes 20% longer.

Thank you so much I’ve been trying to learn to script on the devforum and finally there is a up to date tutorial, thank you a lot.

1 Like

No problem! a part 3 is going to be coming up but it is going to be a bit advanced! If you want to learn more begineer scripting, I would recommend you to watch Alvinblox, or thedevking.

Good luck on your scripting journey!

1 Like

Thank you! I appreciate it very much!

1 Like

No problem!

Good luck! :smiley:

New things to learn ahead in your scripting journey.

1 Like

Always new things to learn. Thanks for the recommendations by the way. If you don’t mind me asking, how’d you learn how to script?

1 Like

How did I learn how to script? I would answer that to watching some of the begineer alvinblox tutorials, and everyday maybe watch 1 or 2 to not burn yourself out. Then, when I got free time, I would go to studio, and just make random things, such as a killbrick with instancing or a part that changes color every second. Then, I would watch advanced tutorials and repeat. Whenever, I forget something I would go back and watch it. Any errors you find while scripting, and don’t get how to fix, I would try my best to fix or just ask the dev forum as there are many talented scripters.

This was a long comment. But hope it helped! :heart:

1 Like

Thank you. I’ve asked the devforum and they have given lots of tips on how to learn, and you also helped. Again, I appreciate it.

No problem! Good luck!

:heart:

Well, that all now, so I got to do something, talk to you later, also want to collab or something?

1 Like

Yeah sure! By the way if you wanna talk more or something message me privately!

1 Like

Great tutorial! I don’t think there’s anything to criticise here!