Scripting: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Outofspace
m (Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">")
Tags: Mobile edit Mobile web edit
 
(107 intermediate revisions by 27 users not shown)
Line 1: Line 1:
{{CatUp|Lua Help}}
{{EmphasisBox|<big>For beginner tutorials, go to the [[Lua Help]] page.</big>|green|dark=yes}}


So, you want to learn how to script? This should be the very first place to look. Be sure to read everything carefully, making sure that you understand what is being read. If you have any questions or comments feel free to ask them either [[Talk:Scripting|on the discussion page]] or on the [http://www.roblox.com/Forum Forum].
This article is a portal for Roblox scripting and related articles.


==Introduction==
==Lua Programming Language==


The ROBLOX scripting language is [http://www.lua.org Lua 5.1], a concise and efficient scripting language used in games and embedded systems. Documentation on the language can be found at [http://www.lua.org/docs.html here].  
ROBLOX uses [http://www.lua.org Lua 5.1], a simple scripting language that can be embedded into games or programs (parent applications). ROBLOX developers have added in functionality to Lua so that users can create interactive content, like tools, buttons, leaderboards, and more.


Scripting requires the [[Script]] object which can control a number of things. For list of Lua objects that can be used, see the [[Class Reference]].
* [http://www.lua.org Lua Website]
* [http://www.lua.org/pil Programming in Lua (first edition)]
* [http://www.lua.org/docs.html Lua Documentation]


This article is documentation for people slightly familiar with the language. If you have no idea what you're doing, you may want to look at these:
==Tutorials==


*[[Absolute beginner's guide to scripting]]
The Roblox Wiki offers many tutorials for learning Lua.
*[[Flowcharts]]


[[Intro to Scripting: Make a Dance Floor]] is a tutorial for beginners that shows how [[Script|scripts]] can be used to control the [[BrickColor (Property)|color]] of [[Part|parts]]. Also, when scripting it is also important to look at the [[Object Browser]]. Keep it open when you're making scripts in [[Edit|Edit mode]]. It can show where and why you've made a mistake.
*[[Lua Help]]
*[[Scripting_Book|Scripting Book]]


<!--
==Fundamentals==
==Fundamentals==


Line 21: Line 24:


===Defining Scripts===
===Defining Scripts===
 
  !Add to [[Script]]
Script objects can be placed under the [[RBX.lua.Workspace (Service)|Workspace]] or under [[Part|parts]], [[Model|models]], [[RBX.lua.StringValue (Object)|values]], etc.
Script objects can be placed under the [[RBX.lua.Workspace (Service)|Workspace]] or under [[Part|parts]], [[Model|models]], [[RBX.lua.StringValue (Object)|values]], etc.


  !Add to [[Command Bar]]
Scripts can also be written and executed in the Command toolbar. To execute a script from the command bar:
Scripts can also be written and executed in the Command toolbar. To execute a script from the command bar:


#Edit a place in [[Roblox Studio|ROBLOX Studio]].<br/><br/>  
#Edit a place in [[Roblox Studio|ROBLOX Studio]].<br/><br/>  
#Select View-->Toolbars-->Command from the menu bar.  
#Select View->Toolbars->Command from the menu bar.  
#:''The Command toolbar will appear at the bottom of the screen.''<br/><br/>  
#:''The Command toolbar will appear at the bottom of the screen.''<br/><br/>  
#Select View-->[[Output]] from the menu bar.  
#Select View->[[Output]] from the menu bar.  
#:''The Output panel will appear just above the Command toolbar.''<br/><br/>  
#:''The Output panel will appear just above the Command toolbar.''<br/><br/>  
#In the Command toolbar's textbox type the following:
#In the Command toolbar's textbox type the following:
Line 37: Line 41:


===Script Execution===
===Script Execution===
 
  !Add to [[Script]]/explain in a tutorial
Script objects only run under certain conditions:
Script objects only run under certain conditions:


* Scripts entered in the Command toolbar execute when you press Enter. This only applies if you're in wither edit mode or [[Solo mode]].
* Scripts entered in the Command toolbar execute when you press Enter. This only applies if you're in either edit mode or [[Solo mode]].


* Scripts will run, as said before, if under the [[RBX.lua.Workspace (Service)|Workspace]] or general containers.
* Scripts will run, as said before, if under the [[RBX.lua.Workspace (Service)|Workspace]] or general containers.
Line 46: Line 50:
* Scripts inside a Tool run when the Tool is activated.
* Scripts inside a Tool run when the Tool is activated.


Also, if you want a script to NOT run when the game is started, make sure that under the [[Properties]] for a script, the [[Bool]] 'Disabled', is set to true. A script will also not run if under the [[Lighting]], [[Sound Service]], or any [[Service]] besides the [[RBX.lua.Workspace (Service)|Workspace]]. If the script's [[Parent (Property)|property]] is under the [[DataModel]], then it will also no run.
* Scripts saved to your computer can be ran using 'Execute script' in ROBLOX studio, which is under the Tools menu.
 
Also, if you want a script to NOT run when the game is started, make sure that the [[Disabled (Property)|Disabled]] [[Properties|property]] is set to true. A script will not run if under the [[Lighting]], [[Sound Service]], or any [[Service]] besides the [[RBX.lua.Workspace (Service)|Workspace]], including the [[DataModel]].
-->


===Environments and Libraries===
==Environments and Libraries==


Each Script has its own copy of the global [http://www.lua.org/manual/5.1/manual.html#2.9  Environment]. This "sandboxes" each script. For instance another script cannot overwrite the global data defined in another script.
An environment can be described as a place where variables live. Each script has its own environment.


ROBLOX loads the following Lua standard libraries: [http://www.lua.org/manual/5.1/manual.html#5.1  Basic Functions], [http://www.lua.org/manual/5.1/manual.html#5.2 Coroutine Manipulation],  [http://www.lua.org/manual/5.1/manual.html#5.4 String Manipulation],  [http://www.lua.org/manual/5.1/manual.html#5.5 Table Manipulation],  [http://www.lua.org/manual/5.1/manual.html#5.6 Mathematical Functions]. For security purposes some functions have been removed to deny access to system resources. A list of these functions can be found in the [[Function Dump]] page of the wiki. In addition, some [http://wiki.roblox.com/index.php?title=Category:ROBLOX_Lua_Objects Roblox Lua Objects] have been removed for the very same reason.
*[[Environment]]


===Math===
<!-- Add to [[Environment]]
This is so another script cannot overwrite the data defined in another script. However, each script also has access to the global environment, so it can use the functions and libraries located there. Note that a change made to the global environment by one script will affect all scripts, since they all access the same global environment.
-->


One important feature that nearly all programming languages use is math. Here's what you need to know:


*You can use the [[Output]] to view your solutions. You can use other tyoes of outout too, like [[RBX.lua.Message (Object)|messages]] and [[RBX.lua.Hint (Object)|hints]].
A library is a group of functions that can be used to make scripting easier. For security purposes, some functions have been removed from Roblox to prevent access to system resources.
*Roblox can perform complex probelms for you.
*This can make things extreemly simple.


Here's a sample of basic math. Open up [[Roblox Studio]], goto File> New, insert a [[Script]] into the [[RBX.lua.Workspace (Service)|Workspace]], make sure that the [[Output]] is open.  
ROBLOX loads the ''coroutine'', ''string'', ''table'', ''math'', and basic function libraries.


{{Example|
*[[Function Dump]]
<pre>
a = 1 + 1 --'a' represents your math problem.
print(a) --This prints the result.
</pre>
<pre>
a = 5*5+1
print(a)
</pre>
<pre>
a = (9*9) + 19
print(a)
</pre>
}}


For more information, and usage, read the following articles:
==Math==
<!--Revise-->
One important feature that all programming languages use is math. For more information, and usage, read the following articles:


*[[Basic math]]
*[[Basic math]]
Line 87: Line 82:
*[[How To Increase and Decrease Vector3 Values]]
*[[How To Increase and Decrease Vector3 Values]]


==Data Model==
==Types==
<i>The [[DataModel]] is what you would define as 'game' in your scripts.</i> <br>


Here are Lua 'types' that can be used in a script
All values in Lua have their own type. These values can be stored in variables, passed as arguments to other functions, and returned as results.
 
*[[Data Types]]


===Basic Types===
===Basic Types===


ROBLOX scripts use the basic [http://www.lua.org/manual/5.1/manual.html#2.2 Lua types] ''[[nil]], [[Bool|boolean]], number, [[string]], function, userdata, [[thread]], and [[Tables|table]]''. In addition, there are the following additional basic types:
Every value in Lua will be one of these types:


====Vector3====
*[[nil]]
*[[Bool|boolean]]
*[[number]]
*[[string]]
*[[function]]
*[[userdata]]
*[[thread]]
*[[Tables|table]]
 
===Roblox-Defined Types===


[[Vector3]] represents a 3D vector. All [[properties]] are [[Read-Only]].
These are special types created by and available only in Roblox.
{| border="1"
! Property !! Type !! Description
|-
| <code>x</code> || number ||| the x-coordinate
|-
| <code>y</code> || number ||| the y-coordinate
|-
| <code>z</code> || number ||| the z-coordinate
|-
| <code>unit</code> || Vector3 ||| a normalized copy of the vector
|-
| <code>magnitude</code>|| number ||| the length of the vector
|}


*[[Vector3]] -- Represents 3-dimensional coordinates. These are used for things like a brick's size or position.
*[[Vector2]] -- Represents 2-dimensional coordinates.
*[[CFrame]] -- Holds a position and a rotation. These define the rotation of bricks.
*[[Color3]] -- Makes colors using an RGB format.
*[[BrickColor]] -- Defines a specific color for bricks.
*[[UDim2]] -- Holds a Scale and Offset position for Guis.
<!-- move to respective articles
====Vector3====


{| border="1"
[[Vector3]]s are 3 coordinates put together. They're kind of like a 2D graph with (x, y) coordinates, except that vector3s use 3 instead of 2: (x, y, z). These are used for things like brick size, and position.
! Member Function !! Description
|-
| <code>Vector3 lerp(Vector3 goal, number alpha)</code> || returns a Vector3 lerped between this Vector3 and the goal. alpha should be between 0 and 1
|}


====CFrame====
====CFrame====


[[CFrame]], know as character frame, but more specifically coordinate frame.
Coordinate Frames, or CFrames, are data types that hold a position and a rotation. [[CFrame|Read more about CFrames here]].
 
{| border="1"
! Constructor !! Description
|-
| <code>new()</code> || Creates an identity CFrame
|-
| <code>new(v)</code> || Creates CFrame from offset Vector3 v
|-
| <code>new(v, l)</code> || Creates CFrame from offset Vector3 v looking at point l
|-
| <code>new(x, y, z)</code> || Creates CFrame from offset (x, y, z)
|-
| <code>new(x, y, z, qx, qy, qz, qw)</code> || Creates CFrame from offset (x, y, z) and quaternion (qx, qy, qz, qw)
|-
| <code>new(x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22)</code> || Creates CFrame from offset (x, y, z) and rotation matrix (R00, R01, R02, R10, R11, R12, R20, R21, R22)
|-
| <code>fromEulerAnglesXYZ(x, y, z)</code> || Creates a rotated CFrame (x, y, z) in radians
|-
| <code>fromAxisAngle(v, r)</code> || Creates a rotated CFrame from a unit vector and a rotation in [http://en.wikipedia.org/wiki/Radian radians]
|}
 
 
{| border="1"
! Property !! Type !! Description
|-
| <code>p</code> || Vector3 ||| the translation
|-
| <code>x</code> || number ||| the x-component of translation
|-
| <code>y</code> || number ||| the y-component of translation
|-
| <code>z</code> || number ||| the z-component of translation
|-
| <code>lookVector</code> || Vector3 ||| returns the facing direction along each axis
|}
 
 
{| border="1"
! Member Function !! Description
|-
| <code>CFrame inverse()</code> || returns the inverse of this CFrame
|-
| <code>CFrame toWorldSpace(CFrame)</code> || returns a CFrame transformed from Object to World coordinates. Also works with tuples
|-
| <code>CFrame toObjectSpace(CFrame)</code> || returns a CFrame transformed from World to Object coordinates. Also works with tuples
|-
| <code>Vector3 pointToWorldSpace(Vector3)</code> || returns a Vector3 transformed from Object to World coordinates. Also works with tuples
|-
| <code>Vector3 pointToObjectSpace(Vector3)</code> || returns a Vector3 transformed from World to Object coordinates. Also works with tuples
|-
| <code>Vector3 vectorToWorldSpace(Vector3)</code> || returns a Vector3 rotated from Object to World coordinates. Also works with tuples
|-
| <code>Vector3 vectorToObjectSpace(Vector3)</code> || returns a Vector3 rotated from World to Object coordinates. Also works with tuples
|}
 
 
{| border="1"
! Operator !! Description
|-
| <code>CFrame * CFrame</code> || returns composition of two CFrames
|-
| <code>CFrame * Vector3</code> || returns Vector3 transformed from Object to World coordinates
|-
| <code>CFrame + Vector3</code> || returns CFrame translated by Vector3
|-
| <code>CFrame - Vector3</code> || returns CFrame translated by -Vector3
|}


====Color3====
====Color3====


[[Color3]], sets color likely in [http://en.wikipedia.org/wiki/Hexadecimal Hex] format (e.g. 1, 255, 98).
Color3, is used to make colors. It's called color'''3''' because it uses 3 numbers to create a color. [[Color3|Read more about Color3 here]].


====BrickColor====
====BrickColor====


{| border="1"
BrickColor is a type of color used in Parts and other objects. [[BrickColor|Read more about BrickColor here]].
! Function!! Type !! Description
|-
| <code>new</code> || BrickColor ||| returns a BrickColor (from a number, string, Color3 or r-g-b values)
|-
| <code>Random</code> || BrickColor ||| returns a random BrickColor
|-
| <code>White</code> || BrickColor ||| returns White
|-
| <code>Gray</code> || BrickColor ||| returns Gray
|-
| <code>DarkGray</code> || BrickColor ||| returns Dark Gray
|-
| <code>Black</code> || BrickColor ||| returns Black
|-
| <code>Red</code> || BrickColor ||| returns Red
|-
| <code>Yellow</code> || BrickColor ||| returns Yellow
|-
| <code>Green</code> || BrickColor ||| returns Green
|-
| <code>Blue</code> || BrickColor ||| returns Blue
|-
|}


{| border="1"
====Connection====
! Property !! Type !! Description
|-
| <code>Number</code> || number ||| The unique number that identifies the BrickColor
|-
| <code>Name</code> || string ||| the name associated with the BrickColor
|-
| <code>Color</code> || Color3 ||| the Color3 associated with the BrickColor
|-
| <code>r</code> || number ||| the red component (between 0 and 1)
|-
| <code>g</code> || number ||| the green component (between 0 and 1)
|-
| <code>b</code> || number ||| the blue component (between 0 and 1)
|}
 
<br>Example:<br>
<code>
local b = BrickColor.Yellow()
<br>print(b.r)</code><br>
prints: 0.96078437566757
 
'''BrickColor Codes'''


[[image:RobloxColors.PNG]]
Represents a connection to an [[#Events|Event]].
[+] Use SirGelatina's [[Color Reference]]
-->


{{Example|1=<i></i>
==Global Functions==
<pre>
<!--Move to [[Function Dump]] as separate section-->
-- Set myPart color to red.
Roblox defines the following global variables (and <strong>many</strong> others):
myPart.BrickColor = BrickColor.new(21)


-- Note that this is equivalent to:
* <syntaxhighlight lang="lua">game</syntaxhighlight> is a reference to the [[DataModel]] instance that is the root of the object hierarchy.
myPart.Color = Color.new(1,0,0)
* <syntaxhighlight lang="lua">Workspace</syntaxhighlight> is a shortcut for the Workspace instance that is a child of the DataModel.
* <syntaxhighlight lang="lua">script</syntaxhighlight> is a reference to the Script instance that is the owner of the referencing code. (not applicable for the Command toolbar)
* <syntaxhighlight lang="lua">print(...)</syntaxhighlight> echoes each argument to the output panel.
* <syntaxhighlight lang="lua">error(e,level)</syntaxhighlight> the Roblox implementation of error is much the same as print, echoing the first argument (in red text) to the output panel, only raising an error as well.
* <syntaxhighlight lang="lua">time()</syntaxhighlight> returns the current game time in seconds. Is simply an accessor for the game's internal time count, such that the statement <syntaxhighlight lang="lua">time() == time()</syntaxhighlight> is true.
* <syntaxhighlight lang="lua">tick()</syntaxhighlight> returns the OS time in seconds, queries the OS every time it is called, such that the statement <syntaxhighlight lang="lua">tick() == tick()</syntaxhighlight> is false.
* <syntaxhighlight lang="lua">wait(t)</syntaxhighlight> yields the current thread and resumes it after "t" seconds have elapsed. If t is not specified, then it yields for one frame (approx. 1/30 sec). The function returns 2 values: The elapsed time and the current game time.
* <syntaxhighlight lang="lua">delay(t, f)</syntaxhighlight> Asynchronously executes function "f" after "t" seconds have elapsed. The function "f" is called with 2 arguments:  The elapsed time and the current game time.
* <syntaxhighlight lang="lua">Spawn(f)</syntaxhighlight> This function ''spawns'' a new thread. Argument "f" is a function that will execute in a new [[Thread]].
* <syntaxhighlight lang="lua">LoadLibrary(library)</syntaxhighlight> Loads the library "library." The argument to this function is a string. The most commonly loaded library is [[RbxGui]].


-- Which is also equivalent to:
==Roblox Objects==
myPart.BrickColor = BrickColor.new("Bright red")
</pre>
}}


====Connection====
Objects are what make Roblox work. Everything seen in-game is an object, as well as everything visible in the [[Explorer]] panel. All Objects have [[Properties]], [[Functions]] and [[Events]].
Represents a connection to an [[#Events|Event]]
;


===Globals===
*[[Class Reference]]
In addition to loading several [[Environments and Libraries|Standard Libraries]] ROBLOX defines the following global variables:
 
* '''<code>game</code>''' is the DataModel object that is the root of the tree for a Place.
* '''<code>workspace</code>''' is the Workspace object that is a child of the DataModel.
* '''<code>script</code>''' is the Script object that defines the script.  (not applicable for the Command toolbar)
* '''<code>print()</code>''' echoes each argument to the Output panel.
* '''<code>time()</code>''' returns returns the current game time in seconds.  (NOTE: THis function is scheduled for an upcoming release)
* '''<code>wait(t)</code>''' yields the current thread after "t" seconds have elapsed.  If t is not specified, then it yields for a very short period of time. The function returns 2 values:  The elapsed time and the current game time.
* '''<code>delay(t,f)</code>''' executes function "f" after "t" seconds have elapse. The function "f" is called with 2 arguments:  The elapsed time and the current game time.
 
===Roblox Objects===
<i>See Main List: [http://wiki.roblox.com/index.php?title=Category:ROBLOX_Lua_Objects RBX.lua Objects]</i>
 
Each object in the [[Explorer]] is accessible with scripting. ROBLOX Objects have [[Properties]], [[Functions]] and [[Scripting#Events|Events]]. You can browse the member definitions by clicking [[Class Explorer]] under the Help menu.


<!--
====Properties====
====Properties====
Object properties are accessed with the “dot” notation:
Object properties are accessed with the “dot” notation:
Line 303: Line 177:


<div style="border: solid #aaa 1px; background: #f9f9f9; padding: 4px; text-align: left;">
<div style="border: solid #aaa 1px; background: #f9f9f9; padding: 4px; text-align: left;">
'''Note:''' If you are used to other scripting languages you will need remember to use <code>:</code> instead of <code>.</code> when invoking a member function. <code>script.clone()</code> will not behave as you might expect.</div>
'''Note:''' If you are used to other scripting languages you will need remember to use <code>:</syntaxhighlight> instead of <code>.</syntaxhighlight> when invoking a member function. <code>script.clone()</syntaxhighlight> will not behave as you might expect.</div>


====Events====
====Events====
Line 310: Line 184:
There are two ways to handle an Event:
There are two ways to handle an Event:


# When a script calls <code>wait()</code>, the currently executing thread yields until the Event is fired. The return values from <code>wait()</code> are the Event arguments.<br><br>
# When a script calls <code>wait()</syntaxhighlight>, the currently executing thread yields until the Event is fired. The return values from <code>wait()</syntaxhighlight> are the Event arguments.<br><br>
# A script calls <code>connect()</code> passing in a function that the Event will call each time the Event is fired. <code>connect()</code> immediately returns a Connection object that can be used to disconnect the listener function.
# A script calls <code>connect()</syntaxhighlight> passing in a function that the Event will call each time the Event is fired. <code>connect()</syntaxhighlight> immediately returns a Connection object that can be used to disconnect the listener function.




Line 332: Line 206:
-- Stop listening to the Touched event
-- Stop listening to the Touched event
connection:disconnect()</pre>
connection:disconnect()</pre>
This code listens for two events: The Part being touched and also for a ''Changed'' event. Since we connected to the Touched event, the function <code/>onTouched()</code> will be called each time Part is touched. However, once the ''Changed'' Event fires and the <code>wait()</code>
This code listens for two events: The Part being touched and also for a ''Changed'' event. Since we connected to the Touched event, the function <code>onTouched()</syntaxhighlight> will be called each time Part is touched. However, once the ''Changed'' Event fires and the <code>wait()</syntaxhighlight>
call returns, we won't listen for another ''Changed'' event. At that point we also disconnect from the ''Touched'' event.
call returns, we won't listen for another ''Changed'' event. At that point we also disconnect from the ''Touched'' event.


Line 339: Line 213:
== Object Types ==
== Object Types ==
There are many different object types in Roblox, and in addition to adding them through the Roblox Studio, you can also define a new object in a script with the line Instance.new("className"), with className being the object's type. Following is a listing of many of the various object types.
There are many different object types in Roblox, and in addition to adding them through the Roblox Studio, you can also define a new object in a script with the line Instance.new("className"), with className being the object's type. Following is a listing of many of the various object types.
-->


==Common Mistakes==


==Common Mistakes==
The slightest misspelling, or even incorrect capitalization will result in your script failing to work. REMEMBER: Look at the [[Output]] to check for errors. If this doesn't help, back-track and make sure everything is perfect. If you think there is a problem, or you need help in general, request help [http://www.roblox.com/Forum on the Forum.]


The slightest misspelling, or even incorrect capitalization will result in your script failing to work. REMEMBER: Look at the [[Output]] to check for errors. If this doesn't help, back-track and make sure everything is perfect. If you think there is a problem, or you need help in general, report the problem to the [[Bugs]] article, and request help [http://www.roblox.com/Forum on the Forum.] For more help, see the 'See Also' section at the bottom of the page.
See [[Common Scripting Mistakes]] and [[Debugging]]. For more help, see the 'See Also' section at the bottom of the page.


==See Also==
==See Also==
Line 349: Line 225:
<b><i>In the beginning:...</i></b>
<b><i>In the beginning:...</i></b>


*[[Your first script]]
*[[Your First Script]]
*[[Basic Scripting]]
*[[Basic Scripting]]
*[[Explaining Scripts]]
*[[Loops]]
*[[Loops]]
*[[How To Increase and Decrease Vector3 Values]]
*[[How To Increase and Decrease Vector3 Values]]
Line 358: Line 233:
<b>Scripting [[Tutorials]] and Articles:</b>
<b>Scripting [[Tutorials]] and Articles:</b>


*[[Commands]]
*[[Intro to Scripting: Make a Healing Potion]]
*[[In-Depth Scripting Guide]]
*[[In-Depth Scripting Guide]]
*[[Intro to Scripting: Make an Invisibility Tool]]
*[[Teleportation]]
*[[Teleportation]]
*[[How to make lava]]
*[[How to Make Weapon Giving Teleporters]]
*[[How Do I Make Morphs?]]
*[[How to Create a Black Hole]]
*[[How to Make Conveyor Belts]]  
*[[How to Make Conveyor Belts]]  
*[[How to Make a Plane]]
*[[How to make circles]]  
*[[How to make circles]]  
*[[How to make a spiral staircase]]
 
*[[How to make a double helix]]
<b>General Information:</b>
<b>General Information:</b>


*[[Random numbers]]
*[[Random numbers]]
*[[Commands]]
*[[Conditional statements]]  
*[[Conditional statements]]  
*[[Variables]]
*[[Variables]]
Line 383: Line 247:
*[[Bool]]  
*[[Bool]]  
*[[String]]  
*[[String]]  
*[[Flowcharts]]
*[[Linked lists]]
*[[Linked lists]]
*[[Functions]]
*[[Functions]]
Line 390: Line 253:
<b>Support:</b>
<b>Support:</b>


*[[Request Lua Feature]]
*[[Bug log]]
*[[Bugs]]


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]
[[Category:Reference Pages]]
[[Category:Reference Pages]]

Latest revision as of 01:06, 27 April 2023

For beginner tutorials, go to the Lua Help page.

This article is a portal for Roblox scripting and related articles.

Lua Programming Language

ROBLOX uses Lua 5.1, a simple scripting language that can be embedded into games or programs (parent applications). ROBLOX developers have added in functionality to Lua so that users can create interactive content, like tools, buttons, leaderboards, and more.

Tutorials

The Roblox Wiki offers many tutorials for learning Lua.


Environments and Libraries

An environment can be described as a place where variables live. Each script has its own environment.


A library is a group of functions that can be used to make scripting easier. For security purposes, some functions have been removed from Roblox to prevent access to system resources.

ROBLOX loads the coroutine, string, table, math, and basic function libraries.

Math

One important feature that all programming languages use is math. For more information, and usage, read the following articles:

Types

All values in Lua have their own type. These values can be stored in variables, passed as arguments to other functions, and returned as results.

Basic Types

Every value in Lua will be one of these types:

Roblox-Defined Types

These are special types created by and available only in Roblox.

  • Vector3 -- Represents 3-dimensional coordinates. These are used for things like a brick's size or position.
  • Vector2 -- Represents 2-dimensional coordinates.
  • CFrame -- Holds a position and a rotation. These define the rotation of bricks.
  • Color3 -- Makes colors using an RGB format.
  • BrickColor -- Defines a specific color for bricks.
  • UDim2 -- Holds a Scale and Offset position for Guis.

Global Functions

Roblox defines the following global variables (and many others):

  • game
    
    is a reference to the DataModel instance that is the root of the object hierarchy.
  • Workspace
    
    is a shortcut for the Workspace instance that is a child of the DataModel.
  • script
    
    is a reference to the Script instance that is the owner of the referencing code. (not applicable for the Command toolbar)
  • print(...)
    
    echoes each argument to the output panel.
  • error(e,level)
    
    the Roblox implementation of error is much the same as print, echoing the first argument (in red text) to the output panel, only raising an error as well.
  • time()
    
    returns the current game time in seconds. Is simply an accessor for the game's internal time count, such that the statement
    time() == time()
    
    is true.
  • tick()
    
    returns the OS time in seconds, queries the OS every time it is called, such that the statement
    tick() == tick()
    
    is false.
  • wait(t)
    
    yields the current thread and resumes it after "t" seconds have elapsed. If t is not specified, then it yields for one frame (approx. 1/30 sec). The function returns 2 values: The elapsed time and the current game time.
  • delay(t, f)
    
    Asynchronously executes function "f" after "t" seconds have elapsed. The function "f" is called with 2 arguments: The elapsed time and the current game time.
  • Spawn(f)
    
    This function spawns a new thread. Argument "f" is a function that will execute in a new Thread.
  • LoadLibrary(library)
    
    Loads the library "library." The argument to this function is a string. The most commonly loaded library is RbxGui.

Roblox Objects

Objects are what make Roblox work. Everything seen in-game is an object, as well as everything visible in the Explorer panel. All Objects have Properties, Functions and Events.


Common Mistakes

The slightest misspelling, or even incorrect capitalization will result in your script failing to work. REMEMBER: Look at the Output to check for errors. If this doesn't help, back-track and make sure everything is perfect. If you think there is a problem, or you need help in general, request help on the Forum.

See Common Scripting Mistakes and Debugging. For more help, see the 'See Also' section at the bottom of the page.

See Also

In the beginning:...

Scripting Tutorials and Articles:

General Information:

Support: