Instance.new
To create new objects through a script, you Instance them. You can create bricks, messages, and even explosions! Instance.new is a simple function takes a string as the argument of what you want to create. Let's get started!
part = Instance.new("Part") -- Now we have the default brick floating in memory.
part.Parent = workspace -- Now the part is in the workspace
part.Size = Vector3.new(15, 15, 15) -- Now our part is a 15x15x15 cube
Easy enough, right? You can Instance anything in the Insert>Object menu.
Syntactic Sugar
We can also parent what we Instance in the same line we create it with:
Instance.new("Explosion", workspace.Part)
-- Puts an explosion in the part we made earlier.
This is an example of syntactic sugar - Syntax that saves time or makes things easier to read. You can even assign properties in the same line you Instance something:
Instance.new("Hint", workspace).Text = "Hello there!"
Further reading: ROBLOX Wiki
part = Instance.new("Part") -- Now we have the default brick floating in memory.
part.Parent = workspace -- Now the part is in the workspace
part.Size = Vector3.new(15, 15, 15) -- Now our part is a 15x15x15 cube
Easy enough, right? You can Instance anything in the Insert>Object menu.
Syntactic Sugar
We can also parent what we Instance in the same line we create it with:
Instance.new("Explosion", workspace.Part)
-- Puts an explosion in the part we made earlier.
This is an example of syntactic sugar - Syntax that saves time or makes things easier to read. You can even assign properties in the same line you Instance something:
Instance.new("Hint", workspace).Text = "Hello there!"
Further reading: ROBLOX Wiki
Methods
Methods are useful commands in RBX.Lua which allow you to make things happen. For example, workspace.Part:Remove() removes the part from workspace, as well as anything inside of it. You can remove almost anything in Explorer. Here are some other common methods:
BreakJoints() breaks the joints of an object. workspace.Model:BreakJoints() breaks the model's joints.
Clone() clones an object. clone = script.Parent:Clone() clone.Parent = workspace clones the parent of the script, and puts it in the workspace.
FindFirstChild() finds the first child of an object with the specified name. workspace:FindFirstChild("My Part") searches for "My Part" in workspace. If "My Part" is there, it will return true. Otherwise, it will return false.
An extra argument can be added to this method. If you add true as an argument, FindFirstChild() will search inside of objects, (most likely models) instead of whats only directly inside of what you're searching in. For example,
workspace:FindFirstChild("My Part", true) will search through the contents of every model and object in Workspace!
GetChildren() creates a table of an object's children:
children = workspace:GetChildren()
It is often used with for loops to loop through the children of a model. You will learn more about this soon.
IsA()helps find the type of object that an object is. For example,
workspace.Brick:IsA("Part") is probably true. You can test any type of object.
IsInGroup() checks to see if a player is in a group. To use it, the player must be in the game. You'll also need to specify the Group ID, which is 3829 in the example below. This method will return a boolean; true if the player is in the group, false if it isn't. For example,
print(game.Players.TheDarkFireDragon:IsInGroup(3829)) prints false to the output.
I hope you'll use methods in your scripts! I highly recommend checking out the link below to learn about more cool methods.
Further reading: ROBLOX Wiki
BreakJoints() breaks the joints of an object. workspace.Model:BreakJoints() breaks the model's joints.
Clone() clones an object. clone = script.Parent:Clone() clone.Parent = workspace clones the parent of the script, and puts it in the workspace.
FindFirstChild() finds the first child of an object with the specified name. workspace:FindFirstChild("My Part") searches for "My Part" in workspace. If "My Part" is there, it will return true. Otherwise, it will return false.
An extra argument can be added to this method. If you add true as an argument, FindFirstChild() will search inside of objects, (most likely models) instead of whats only directly inside of what you're searching in. For example,
workspace:FindFirstChild("My Part", true) will search through the contents of every model and object in Workspace!
GetChildren() creates a table of an object's children:
children = workspace:GetChildren()
It is often used with for loops to loop through the children of a model. You will learn more about this soon.
IsA()helps find the type of object that an object is. For example,
workspace.Brick:IsA("Part") is probably true. You can test any type of object.
IsInGroup() checks to see if a player is in a group. To use it, the player must be in the game. You'll also need to specify the Group ID, which is 3829 in the example below. This method will return a boolean; true if the player is in the group, false if it isn't. For example,
print(game.Players.TheDarkFireDragon:IsInGroup(3829)) prints false to the output.
I hope you'll use methods in your scripts! I highly recommend checking out the link below to learn about more cool methods.
Further reading: ROBLOX Wiki