Scripting: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>GoldenUrg
→‎Common Mistakes: Added links
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">"
Tags: mobile web edit mobile edit
 
(68 intermediate revisions by 19 users not shown)
Line 1: Line 1:
{{EmphasisBox|<big>For beginner tutorials, go to the [[Lua Help]] page.</big>|green|dark=yes}}
This article is a portal for Roblox scripting and related articles.
This article is a portal for Roblox scripting and related articles.


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


Roblox uses [http://www.lua.org Lua 5.1], a concise and efficient scripting language used in games and embedded systems. Lua gives users the ability to create customized content beyond bricks, such as tools, weapons, vehicles, enhanced gameplay, and much more.
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.
 
*[http://www.lua.org/ Lua Website]
*[http://www.lua.org/docs.html Documentation]


* [http://www.lua.org Lua Website]
* [http://www.lua.org/pil Programming in Lua (first edition)]
* [http://www.lua.org/docs.html Lua Documentation]


==Tutorials==
==Tutorials==
Line 14: Line 16:


*[[Lua Help]]
*[[Lua Help]]
*[[Scripting_Book|Scripting Book]]


<!--
<!--
Line 46: Line 49:


* Scripts inside a Tool run when the Tool is activated.
* Scripts inside a Tool run when the Tool is activated.
* 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]].
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]].
Line 52: Line 57:
==Environments and Libraries==
==Environments and Libraries==


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


*[[Environment]]
*[[Environment]]


<!--Add to [[Environment]]
<!-- 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.
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.
-->
-->
Line 66: Line 71:


*[[Function Dump]]
*[[Function Dump]]


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


*[[Basic math]]
*[[Basic math]]
Line 77: Line 81:
*[[Random numbers]]
*[[Random numbers]]
*[[How To Increase and Decrease Vector3 Values]]
*[[How To Increase and Decrease Vector3 Values]]


==Types==
==Types==
Line 103: Line 106:


*[[Vector3]] -- Represents 3-dimensional coordinates. These are used for things like a brick's size or position.
*[[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.
*[[CFrame]] -- Holds a position and a rotation. These define the rotation of bricks.
*[[Color3]] -- Makes colors using an RGB format.
*[[Color3]] -- Makes colors using an RGB format.
*[[BrickColor]] -- Defines a specific color for bricks.
*[[BrickColor]] -- Defines a specific color for bricks.
 
*[[UDim2]] -- Holds a Scale and Offset position for Guis.
<!-- move to respective articles
<!-- move to respective articles
====Vector3====
====Vector3====
Line 131: Line 135:
==Global Functions==
==Global Functions==
<!--Move to [[Function Dump]] as separate section-->
<!--Move to [[Function Dump]] as separate section-->
Roblox defines the following global variables:
Roblox defines the following global variables (and <strong>many</strong> others):


* '''<code>game</code>''' is the DataModel object that is the root of the tree for a Place.
* <syntaxhighlight lang="lua">game</syntaxhighlight> is a reference to the [[DataModel]] instance that is the root of the object hierarchy.
* '''<code>workspace</code>''' is the Workspace object that is a child of the DataModel.
* <syntaxhighlight lang="lua">Workspace</syntaxhighlight> is a shortcut for the Workspace instance that is a child of the DataModel.
* '''<code>script</code>''' is the Script object that defines the script. (not applicable for the Command toolbar)
* <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)
* '''<code>print()</code>''' echoes each argument to the Output panel.
* <syntaxhighlight lang="lua">print(...)</syntaxhighlight> echoes each argument to the output panel.
* '''<code>time()</code>''' returns the current game time in seconds. (NOTE: THis function is scheduled for an upcoming release)
* <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.
* '''<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.
* <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.
* '''<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.
* <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]].


==Roblox Objects==
==Roblox Objects==
Line 169: 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 176: 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 198: 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 217: 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 226: 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 251: Line 247:
*[[Bool]]  
*[[Bool]]  
*[[String]]  
*[[String]]  
*[[Flowcharts]]
*[[Linked lists]]
*[[Linked lists]]
*[[Functions]]
*[[Functions]]
Line 258: 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 05: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: