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:
- Function Parameters
- Instancing
- Variables
- FindFirstChild
- 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.