How do I create a brick?: Difference between revisions
>Flurite |
>NXTBoy →Adding New Objects (Instances): Stop using Template:Example for a code snippet! |
||
Line 9: | Line 9: | ||
Now, to add a new object, we need to use the term 'Instance.new'. This term creates a new [[Instance|instance]]. An instance is a longer term for an object. Type this in the command bar, then press enter to allow the code to execute. | Now, to add a new object, we need to use the term 'Instance.new'. This term creates a new [[Instance|instance]]. An instance is a longer term for an object. Type this in the command bar, then press enter to allow the code to execute. | ||
Instance.new("Part", game.Workspace) | |||
So here in this command, we see the term 'Instance.new()'. So now, we state the objects className, which is in this case, "Part". Because whenever you create a new object, its parent is nil (nothing), we have to set it somewhere. You could put the parent of the newly created object right inside the parenthesis, separated by a comma. If you don't choose where to place the part, you can do it later. | So here in this command, we see the term 'Instance.new()'. So now, we state the objects className, which is in this case, "Part". Because whenever you create a new object, its parent is nil (nothing), we have to set it somewhere. You could put the parent of the newly created object right inside the parenthesis, separated by a comma. If you don't choose where to place the part, you can do it later. |
Revision as of 22:12, 28 July 2011
Object Creation via Script
It is time to learn how to create different objects via script. Instead of using "Insert > Object.. > Script", a script line can be used. Almost anything and everything can be created via scripts, even a whole new functioning script.
Adding New Objects (Instances)
Now, to add a new object, we need to use the term 'Instance.new'. This term creates a new instance. An instance is a longer term for an object. Type this in the command bar, then press enter to allow the code to execute.
Instance.new("Part", game.Workspace)
So here in this command, we see the term 'Instance.new()'. So now, we state the objects className, which is in this case, "Part". Because whenever you create a new object, its parent is nil (nothing), we have to set it somewhere. You could put the parent of the newly created object right inside the parenthesis, separated by a comma. If you don't choose where to place the part, you can do it later.
If you look in the Explorer in the menu, and open up the contents of Workspace, you should see a newly made Script that you made with the previous command.
Editing New Objects
In this section, I will be teaching you how to edit objects when using 'Instance.new()'. This is also where 'local' comes in handy.
local brick = Instance.new("Part", game.Workspace) -- Creating the brick, and placing it into the Workspace brick.Name = "NewBrick" -- This name has to correspond with the name of your brick on the map brick.Size = Vector3.new(10, 10, 10) -- This is the size to which you want to change your brick
Sometimes when doing things like this, the list of things you edited can become really long, sometimes I edit every single property in an object. This could happen to you, so don't worry if your list is much longer than it usually is. Whenever I create a new object and want to edit it, I always make it a local variable. Now we are able to edit its properties by first noting its name, then noting the property. In this specific script, I put its parent into the workspace, then changed its name to "NewBrick", then changed its size to 10x10x10.