LESSON 1 :- Instances and Cloning
First let's start off with “Instances” some of you may have never heard that word in your scripting life so let me explain it, so an instance is a type of thing added to a game so like for an example if you added a part into the workspace/baseplate the part would be considered an “instance” same with others like if you added an NPC, or any part their parts would be considered an “instance”. Now instances are VERY important in scripting as later on we will be using it a lot!
So now you may be wondering how we could make an instance through a script? Well, roblox has a built-in function called “Instance.new()” and we could technically make a part within 1 line of code! So let's dive in!
Instance.new(“Part”) ← Now I want you guys to guess what this could be? … Ok let me tell you
This spawns a part, to determine what to spawn in you have to write it in the brackets inside the Instance.new() “()” ← You need to write it in here using ”” these, you will get a lot of options like explosions and other stuff
Ok so lets say you have made a line called Instance.new(“Part”) and you ran your script, it will not work now let me tell you why, Instance.new() has to be a part of a variable so we could say
local part = Instance.new(“Part”)
But still this won't work because we haven’t parented it to the workspace now if something is not parented to the workspace it will not show in the game it will be there but it won't be in the game if you get what i'm saying, so we could do this
local part = Instance.new(“Part”)
part.Parent = workspace
Now if you ran the script it should’ve worked! A part must have spawned in! If you don't see a part it's possible your spawn point block is blocking it
Now we could use this in a lot of ways!
Like making explosions, making cars, well those are all advanced stuff so don't worry about them now!
Now their are properties of a Part like it's transparency, size etc now I am assuming you know what the properties of a part are so we could use those properties as well, like this
local part = Instance.new(“Part”)
part.Parent = workspace
part.Transparency = 0.5 – This is new!
As you see we used part.Transparency we could also do part.Anchored, etc now let's make the part anchored!
local part = Instance.new(“Part”)
part.Parent = workspace
part.Transparency = 0.5
part.Anchored = True
Now this will spawn in a part with 0.5 transparency and it will be anchored!
Keep in mind you can set the parts position as well like this
part.Position = Vector3.new(10,10,10)
Don't worry about Vector3.new we will talk about that later but it's basically like a 3d position in a way, but the values inside the brackets are where the part would spawn in the workspace! So you can play around with it!
Now let's talk about “Cloning” cloning as you can tell from its name is a clone well now let's take our previous script here
local part = Instance.new(“Part”)
part.Parent = workspace
part.Transparency = 0.5
part.Anchored = True
What we could do is part:Clone() we use :Clone() to clone anything in a game you can use :Clone with any instance in a game to clone it! So technically we could do
local part = Instance.new(“Part”)
part.Parent = workspace
part.Transparency = 0.5
part.Anchored = True
local Clone = part:Clone()
And now if you click run another part will spawn! But you won't see it because we didn’t change the clones position and whenever we spawn a clone it will always spawn at the position of what you cloned so here we cloned this part so the clone would spawn at the “Part” position!
This clones the part we made before now this is why making a variable is important
local part = Instance.new(“Part”)
so we can get that variables value in this case it's a part, and we could clone it or else if we did Instance.new(“Part”) with no variable how could we clone it?? (Keep in mine in this case since the clone is a part we could also change the clones values like its transparency)
Well that's it for today's lesson it was a bit fast paced but feel free to ask any questions down below
Now a quick quiz (Comment below what this script does)
local mypart = Instance.new(“Part”)
mypart.Parent = workspace
mypart.Anchored = False
local Clone = mypart:Clone()
Clone.Anchored = True