Scripting: Difference between revisions
m Text replacement - "</code>" to "</SyntaxHighlight>" |
m Text replacement - "<SyntaxHighlight code="lua">" to "<syntaxhighlight lang="lua">" Tags: mobile web edit mobile edit |
||
(2 intermediate revisions by the same user not shown) | |||
Line 137: | Line 137: | ||
Roblox defines the following global variables (and <strong>many</strong> others): | Roblox defines the following global variables (and <strong>many</strong> others): | ||
* < | * <syntaxhighlight lang="lua">game</syntaxhighlight> is a reference to the [[DataModel]] instance that is the root of the object hierarchy. | ||
* < | * <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]]. | ||
==Roblox Objects== | ==Roblox Objects== | ||
Line 177: | 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>:</ | '''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 184: | Line 184: | ||
There are two ways to handle an Event: | There are two ways to handle an Event: | ||
# When a script calls <code>wait()</ | # 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()</ | # 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 206: | 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()</ | 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. | ||
Latest revision as of 05:06, 27 April 2023
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:
- Basic math
- Variables
- Conditional statements
- Random numbers
- How To Increase and Decrease Vector3 Values
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):
- is a reference to the DataModel instance that is the root of the object hierarchy.
game
- is a shortcut for the Workspace instance that is a child of the DataModel.
Workspace
- is a reference to the Script instance that is the owner of the referencing code. (not applicable for the Command toolbar)
script
- echoes each argument to the output panel.
print(...)
- 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.
error(e,level)
- returns the current game time in seconds. Is simply an accessor for the game's internal time count, such that the statement
time()
is true.time() == time()
- returns the OS time in seconds, queries the OS every time it is called, such that the statement
tick()
is false.tick() == tick()
- 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.
wait(t)
- 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.
delay(t, f)
- This function spawns a new thread. Argument "f" is a function that will execute in a new Thread.
Spawn(f)
- Loads the library "library." The argument to this function is a string. The most commonly loaded library is RbxGui.
LoadLibrary(library)
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:...
- Your First Script
- Basic Scripting
- Loops
- How To Increase and Decrease Vector3 Values
- In-Depth Scripting Guide
Scripting Tutorials and Articles:
General Information:
- Random numbers
- Conditional statements
- Variables
- Flowcharts
- Generic for
- Bool
- String
- Linked lists
- Functions
- Tables
Support: