Common functions: Difference between revisions
>Sncplay42 →The "wait()" Function: clarify |
>Samacado fixed a few (1) error in code, added syntax highlighting, moved TOC, moved a period outside of a parenthesis |
||
Line 1: | Line 1: | ||
==Built-In Functions== | |||
There are many pre-made functions in the Object Browser, but this page will be going through the most-used ones. Most of these functions either use a pre-existing brick or delay something from happening. A final note before you start, all built-in functions start with a ":" and end with a "()". | |||
__TOC__ | __TOC__ | ||
===The "delay()" Function=== | ===The "delay()" Function=== | ||
Line 8: | Line 9: | ||
{{Example| | {{Example| | ||
< | <code lua> | ||
print("Hi!") | print("Hi!") | ||
delay(3, function() | delay(3, function() | ||
print("Bye!") | print("Bye!") | ||
end) | end) | ||
</ | </code> | ||
}} | }} | ||
Line 20: | Line 21: | ||
The wait function will delay, or wait, by a number of seconds. The difference between this and the 'delay()' function is that this stops the '''current''' thread from running, while delay creates a new one. | The wait function will delay, or wait, by a number of seconds. The difference between this and the 'delay()' function is that this stops the '''current''' thread from running, while delay creates a new one. | ||
<code lua> | |||
print("Hi!") | |||
wait(3) | |||
print("Bye!") | |||
</code> | |||
In the output, this prints the word "Hi!", waits three seconds, then prints the word "Bye!". | In the output, this prints the word "Hi!", waits three seconds, then prints the word "Bye!". | ||
Line 32: | Line 35: | ||
This method does it what it says - clones, or makes a copy of, an object. When an object is cloned, the result will have no [[Parent]], so the clone won't exist until you "parent" it to another object. | This method does it what it says - clones, or makes a copy of, an object. When an object is cloned, the result will have no [[Parent]], so the clone won't exist until you "parent" it to another object. | ||
<code lua> | |||
game.Workspace.Brick:Clone().Parent = game.Workspace | |||
</code> | |||
If you are creating a script that makes a [[Part]] that has a [[Decal]] on it, the easier thing to do is to clone the brick, define its position, then parent it. | If you are creating a script that makes a [[Part]] that has a [[Decal]] on it, the easier thing to do is to clone the brick, define its position, then parent it. | ||
Line 41: | Line 46: | ||
You might think you could write a script like this: | You might think you could write a script like this: | ||
<code lua> | |||
script.Parent.Touched:connect(function(part) | |||
local humanoid = part.Parent.Humanoid | |||
if humanoid then | |||
--The part was touched by a player | |||
print("Hello, player!") | |||
end | |||
end | |||
</code> | |||
However, this will error if the part's parent doesn't contain a humanoid. In the following code, humanoid will be nil if there is no humanoid in the part's parent, but it will not error. | However, this will error if the part's parent doesn't contain a humanoid. In the following code, humanoid will be nil if there is no humanoid in the part's parent, but it will not error. | ||
<code lua> | |||
script.Parent.Touched:connect(function(part) | |||
local humanoid = part.Parent:FindFirstChild("Humanoid") | |||
if humanoid then | |||
--The part was touched by a player | |||
print("Hello, player!") | |||
end | |||
end | |||
</code> | |||
===The ":GetChildren()" Method=== | ===The ":GetChildren()" Method=== | ||
This method returns a table of the object's children. The following code shows how it can be used to count all the parts in the workspace | This method returns a table of the object's children. The following code shows how it can be used to count all the parts in the workspace | ||
<code lua> | |||
bricks = 0 | |||
for object in ipairs(game.Workspace:GetChildren()) do | |||
if object:isA("[[BasePart]]") then | |||
bricks = bricks + 1 | |||
end | |||
end | |||
print(bricks) | |||
</code> | |||
===The ":Remove()" Method=== | ===The ":Remove()" Method=== | ||
The Remove function changes the Parent of an object to [[nil]], and also calls Remove on all of its children. | The Remove function changes the Parent of an object to [[nil]], and also calls Remove on all of its children. | ||
<code lua> | |||
game.Workspace.Brick:Remove() | |||
</code> | |||
This method should not be used any more, since there are better alternatives, whatever you want to do. If you want to temporarily take the object out of the game's hierarchy, use ".Parent = nil" to take the object out of the game in a way that you can get it back later. If you want to completely take it out of your game, use the Destroy method. | This method should '''not''' be used any more, since there are better alternatives, whatever you want to do. If you want to temporarily take the object out of the game's hierarchy, use ".Parent = nil" to take the object out of the game in a way that you can get it back later. If you want to completely take it out of your game, use the Destroy method. | ||
=== The ":Destroy()" Method === | === The ":Destroy()" Method === | ||
Unlike the Remove method, the Destroy method completely removes an object. Destroy removes all connections, and locks the object so that it can't be put back into your game. This method is better then :Remove() because it cleans up the memory used by the object. | Unlike the Remove method, the Destroy method completely removes an object. Destroy removes all connections, and locks the object so that it can't be put back into your game. This method is better then :Remove() because it cleans up the memory used by the object. | ||
<code lua> | |||
game.Workspace.Brick:Destroy() | |||
</code> | |||
===The ":MakeJoints()" Method=== | ===The ":MakeJoints()" Method=== | ||
Line 107: | Line 122: | ||
It can be invoked on both parts and models: | It can be invoked on both parts and models: | ||
<code lua> | |||
brick = game.Workspace.Brick | |||
brick:MakeJoints() | |||
</code> | |||
<code lua> | |||
model = game.Workspace.Model | |||
model:MakeJoints() | |||
</code> | |||
===The ":BreakJoints()" Method === | ===The ":BreakJoints()" Method === | ||
This method breaks all joints already made. Running this on a building grouped into a model might cause it to crumble, although bricks stacked on each other will be unaffected until another force acts on them. Once again, this method can be invoked on both models and parts | This method breaks all joints already made. Running this on a building grouped into a model might cause it to crumble, although bricks stacked on each other will be unaffected until another force acts on them. Once again, this method can be invoked on both models and parts | ||
<code lua> | |||
brick = game.Workspace.Brick | |||
brick:BreakJoints() | |||
</code> | |||
<code lua> | |||
model = game.Workspace.Model | |||
model:BreakJoints() | |||
</code> | |||
===The ":GetMass()" Method === | ===The ":GetMass()" Method === | ||
So, ever wondered how heavy a brick is in ROBLOX? The GetMass() method is just for that! It is the same as multiplying all the components of the size of the brick together. NOTE: Editing mass is not possible, as it is a returned value from a method, not a property. | So, ever wondered how heavy a brick is in ROBLOX? The GetMass() method is just for that! It is the same as multiplying all the components of the size of the brick together. NOTE: Editing mass is not possible, as it is a returned value from a method, not a property. | ||
<code lua> | |||
brick = game.Workspace.Brick | |||
print(brick:GetMass()) | |||
--This is the same as... | |||
brick = game.Workspace.Brick | |||
print(brick.Size.X * brick.Size.Y * brick.Size.Z) | |||
</code> | |||
If density existed in ROBLOX, GetMass() would multiply the size components and the density. For the time being, all parts in ROBLOX have a density of 1. | If density existed in ROBLOX, GetMass() would multiply the size components and the density. For the time being, all parts in ROBLOX have a density of 1. | ||
===The ":MoveTo()" Method === | ===The ":MoveTo()" Method === | ||
The MoveTo() function has two different uses. The first is usable on a [[Model]] or any type of [[Part]] (see [[BasePart]]. | The MoveTo() function has two different uses. The first is usable on a [[Model]] or any type of [[Part]] (see [[BasePart]]). | ||
{{Example| | {{Example| | ||
< | <code lua> | ||
--Used on a model... | --Used on a model... | ||
model = game.Workspace.Model | model = game.Workspace.Model | ||
Line 146: | Line 169: | ||
brick = game.Workspace.Brick | brick = game.Workspace.Brick | ||
brick:MoveTo(Vector3.new(0, 0, 10)) | brick:MoveTo(Vector3.new(0, 0, 10)) | ||
</ | </code> | ||
}} | }} | ||
Line 154: | Line 177: | ||
{{Example| | {{Example| | ||
< | <code lua> | ||
Character = game.Workspace.SomePlayer.Humanoid | Character = game.Workspace.SomePlayer.Humanoid | ||
Base = game.Workspace.Base --Assuming your baseplate is named "Base" | Base = game.Workspace.Base --Assuming your baseplate is named "Base" | ||
Character:MoveTo(Vector3.new(15, 0, 10), Base) --Tell Character to talk to (15, 0, 10) on Base. | Character:MoveTo(Vector3.new(15, 0, 10), Base) --Tell Character to talk to (15, 0, 10) on Base. | ||
</ | </code> | ||
}} | }} | ||
Line 165: | Line 188: | ||
{{Example| | {{Example| | ||
< | <code lua> | ||
DB=false | DB=false | ||
function onTouched(hit) | function onTouched(hit) | ||
Line 181: | Line 204: | ||
script.Parent.Touched:connect(onTouched) | script.Parent.Touched:connect(onTouched) | ||
</ | </code> | ||
}} | }} | ||
Line 190: | Line 213: | ||
{{Example| | {{Example| | ||
< | <code lua> | ||
while true do | while true do | ||
wait() | wait() | ||
Line 203: | Line 226: | ||
end | end | ||
</ | </code> | ||
}} | }} | ||
Revision as of 01:28, 14 January 2012
Built-In Functions
There are many pre-made functions in the Object Browser, but this page will be going through the most-used ones. Most of these functions either use a pre-existing brick or delay something from happening. A final note before you start, all built-in functions start with a ":" and end with a "()".
The "delay()" Function
The delay function waits a certain amount of seconds and then executes a function on a new thread.
print("Hi!")
delay(3, function()
print("Bye!")
end)
The "wait()" Function
The wait function will delay, or wait, by a number of seconds. The difference between this and the 'delay()' function is that this stops the current thread from running, while delay creates a new one.
print("Hi!")
wait(3)
print("Bye!")
In the output, this prints the word "Hi!", waits three seconds, then prints the word "Bye!".
See this page for more details on these functions.
Useful methods
The ":Clone()" Method
This method does it what it says - clones, or makes a copy of, an object. When an object is cloned, the result will have no Parent, so the clone won't exist until you "parent" it to another object.
game.Workspace.Brick:Clone().Parent = game.Workspace
If you are creating a script that makes a Part that has a Decal on it, the easier thing to do is to clone the brick, define its position, then parent it.
The ":FindFirstChild()" Method
This method is used to find a child object with a certain name. This is very useful for determining whether something is there or not.
You might think you could write a script like this:
script.Parent.Touched:connect(function(part)
local humanoid = part.Parent.Humanoid
if humanoid then
--The part was touched by a player
print("Hello, player!")
end
end
However, this will error if the part's parent doesn't contain a humanoid. In the following code, humanoid will be nil if there is no humanoid in the part's parent, but it will not error.
script.Parent.Touched:connect(function(part)
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid then
--The part was touched by a player
print("Hello, player!")
end
end
The ":GetChildren()" Method
This method returns a table of the object's children. The following code shows how it can be used to count all the parts in the workspace
bricks = 0
for object in ipairs(game.Workspace:GetChildren()) do
if object:isA("BasePart") then
bricks = bricks + 1
end
end
print(bricks)
The ":Remove()" Method
The Remove function changes the Parent of an object to nil, and also calls Remove on all of its children.
game.Workspace.Brick:Remove()
This method should not be used any more, since there are better alternatives, whatever you want to do. If you want to temporarily take the object out of the game's hierarchy, use ".Parent = nil" to take the object out of the game in a way that you can get it back later. If you want to completely take it out of your game, use the Destroy method.
The ":Destroy()" Method
Unlike the Remove method, the Destroy method completely removes an object. Destroy removes all connections, and locks the object so that it can't be put back into your game. This method is better then :Remove() because it cleans up the memory used by the object.
game.Workspace.Brick:Destroy()
The ":MakeJoints()" Method
MakeJoints forms a bond (a joint) between touching bricks if they have the required surfaces types:
SurfaceType | Weld | Universal | Studs | Inlet | Glue | Smooth |
---|---|---|---|---|---|---|
Smooth | Weld | No Joint | No Joint | No Joint | Glue | No Joint |
Glue | Weld | Glue | Glue | Glue | Glue | |
Inlet | Weld | Snap | Snap | No Joint | ||
Studs | Weld | Snap | No Joint | |||
Universal | Weld | Snap | ||||
Weld | Weld |
It can be invoked on both parts and models:
brick = game.Workspace.Brick
brick:MakeJoints()
model = game.Workspace.Model
model:MakeJoints()
The ":BreakJoints()" Method
This method breaks all joints already made. Running this on a building grouped into a model might cause it to crumble, although bricks stacked on each other will be unaffected until another force acts on them. Once again, this method can be invoked on both models and parts
brick = game.Workspace.Brick
brick:BreakJoints()
model = game.Workspace.Model
model:BreakJoints()
The ":GetMass()" Method
So, ever wondered how heavy a brick is in ROBLOX? The GetMass() method is just for that! It is the same as multiplying all the components of the size of the brick together. NOTE: Editing mass is not possible, as it is a returned value from a method, not a property.
brick = game.Workspace.Brick
print(brick:GetMass())
--This is the same as...
brick = game.Workspace.Brick
print(brick.Size.X * brick.Size.Y * brick.Size.Z)
If density existed in ROBLOX, GetMass() would multiply the size components and the density. For the time being, all parts in ROBLOX have a density of 1.
The ":MoveTo()" Method
The MoveTo() function has two different uses. The first is usable on a Model or any type of Part (see BasePart).
The second use of MoveTo() is to force a Humanoid to walk to a location to the best of its ability. The Humanoid will continue to walk in that direction until it reaches the destination.
This version of MoveTo() requires three arguments, the first is given by the : operator, and is the Humanoid. The second is the target location, and finally, the third is the BasePart the Humanoid is walking on to get to the destination.
The ":GetPlayerFromCharacter()" Method
Ever needed to find out what player(not character) touched your brick in you button script? The ":GetPlayerFromCharacter()" Function makes it easier than actually searching the players with the previous ":FindFirstChild()" and ":GetChildren()" functions. What it does is automatically search an area for a the player that represents the character. If an Instance's "Controller" is "Player", you can use this function.
That script will change give 5 cash to the local stat of cash if you touch it.
The "lookVector" Property
There actually is a CFrame function to find which way you are looking. What this function does is find exactly which way your front is facing. The "lookVector" function can be used for several different situations, e.g., tools, planes, robots, cars, etc.
First is 'while true do' loop. Then is the 'wait()' function. The script finds 'game.Workspace.Player,' (or game.Workspace.(yourusername), then it finds my Torso and sets its velocity to the way my Torso is looking by sixty. This means this character will go at the speed of 60 units per second by the way it is facing.