RBXScriptSignal: Difference between revisions
>JulienDethurens When you're on a redirect page, it shouldn't edit the page it redirected you to when you clicked the edit button, it should edit the redirect page. :/ |
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>" |
||
(25 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
A RBXScriptSignal, | {{redirect|Event|Event (Event)|the Event event of the [[BindableEvent]] object}} | ||
A RBXScriptSignal, more commonly called an ''event'', is a special kind of Roblox Object. It provides a way for user-defined functions to be called when something happens in the game. A RBXScriptSignal is similar to a [[property]], but instead of holding a variable, events fire when a certain event happens. When a RBXScriptSignal fires, functions that are called "handlers", which are connected, or bound, to the event are ran. | |||
==Methods== | ==Methods== | ||
Line 7: | Line 8: | ||
! Method !! Description !! Notes | ! Method !! Description !! Notes | ||
|- | |- | ||
| connect( | | connect({{type|function}} <var>handler</var>)||Establishes a function to be called whenever the event occurs. Returns a {{type|RBXScriptConnection}} object.|| | ||
|- | |- | ||
| connectFirst( | | connectFirst({{type|function}} <var>handler</var>)||Same as connect except handler functions gets called <em>before</em> any other handler.||'''Protected''' and '''Deprecated''' | ||
|- | |- | ||
| connectLast( | | connectLast({{type|function}} <var>handler</var>)||Same as connect except handler functions gets called <em>after</em> all the other handlers.||'''Protected''' and '''Deprecated''' | ||
|- | |- | ||
| wait()||Pauses the script until the event is fired and returns any arguments the event returns.|| | | wait()||Pauses the script until the event is fired and returns any arguments the event returns.|| | ||
|- | |- | ||
| disconnect( | | disconnect()||Can not be used anymore; prints an error message. Use RBXScriptConnection:disconnect() instead.||'''Obsolete''' | ||
|} | |} | ||
Line 31: | Line 32: | ||
To allow our function to use that, we defined it as: | To allow our function to use that, we defined it as: | ||
< | <syntaxhighlight lang="lua"> | ||
function onTouched(otherPart) | function onTouched(otherPart) | ||
--code | --code | ||
end | end | ||
</ | </syntaxhighlight> | ||
Finally, we can use the connection method of events to bind our function to the event. | Finally, we can use the connection method of events to bind our function to the event. | ||
< | <syntaxhighlight lang="lua"> | ||
part.Touched:connect(onTouched) | part.Touched:connect(onTouched) | ||
</ | </syntaxhighlight> | ||
Here's an example using the Touched event of a Part object. | Here's an example using the Touched event of a Part object. | ||
{{Example| | {{Example| | ||
< | <syntaxhighlight lang="lua"> | ||
local part = game.Workspace.Part -- your part here | local part = game.Workspace.Part -- your part here | ||
function onTouched(otherPart) | function onTouched(otherPart) | ||
Line 52: | Line 53: | ||
part.Touched:connect(onTouched) -- call onTouched each time something touches | part.Touched:connect(onTouched) -- call onTouched each time something touches | ||
</ | </syntaxhighlight> | ||
Here we are giving <var>otherPart</var> as an argument to the event handler, onTouched. When <var>part</var> is touched, the part that touches <var>part</var> will turn invisible and <var>part</var> will become a different color. | Here we are giving <var>otherPart</var> as an argument to the event handler, onTouched. When <var>part</var> is touched, the part that touches <var>part</var> will turn invisible and <var>part</var> will become a different color. | ||
}} | }} | ||
Line 58: | Line 59: | ||
== Advanced Usage == | == Advanced Usage == | ||
Any function name is valid: | Any function name is valid: | ||
<syntaxhighlight lang="lua"> | |||
< | |||
function blah() | function blah() | ||
print("In event") | print("In event") | ||
end | end | ||
part.Changed:connect(blah) | part.Changed:connect(blah) | ||
</ | </syntaxhighlight> | ||
Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event once. | Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event once. | ||
<syntaxhighlight lang="lua"> | |||
< | |||
part.Touched:connect(function(otherPart) | part.Touched:connect(function(otherPart) | ||
otherPart.Transparency = 1 | otherPart.Transparency = 1 | ||
part.BrickColor = BrickColor.Random() | part.BrickColor = BrickColor.Random() | ||
end) -- ends the anonymous function and the ''connect'' method call. | end) -- ends the anonymous function and the ''connect'' method call. | ||
</ | </syntaxhighlight> | ||
You can also use anonymous functions to change the arguments: | You can also use anonymous functions to change the arguments: | ||
<syntaxhighlight lang="lua"> | |||
< | |||
player.Chatted:connect(function(message, recipient) | player.Chatted:connect(function(message, recipient) | ||
onChatted(player, message) -- new arguments need to match onChatted function call | onChatted(player, message) -- new arguments need to match onChatted function call | ||
end) | end) | ||
</ | </syntaxhighlight> | ||
Connections can be created at any time, even inside of event functions: | Connections can be created at any time, even inside of event functions: | ||
<syntaxhighlight lang="lua"> | |||
< | |||
function onChatted(player, message) | function onChatted(player, message) | ||
print(player .. " said " .. message) | print(player .. " said " .. message) | ||
Line 100: | Line 94: | ||
game.Players.PlayerAdded:connect(onPlayerAdded) | game.Players.PlayerAdded:connect(onPlayerAdded) | ||
</ | </syntaxhighlight> | ||
== | == Notes == | ||
Connections are automatically disconnected if: | Connections are automatically disconnected if: | ||
* The event handler generates an error before the first wait() call. | * The event handler generates an error before the first wait() call. | ||
* The script itself is removed or reparented. | * The script itself is removed or reparented. | ||
* The object the event relates to is destroyed with the [[ | * The object the event relates to is destroyed with the [[Destroy (Method)|Destroy]] method. | ||
'''Note''': The connect method does not check that the argument is a function/is callable. If you give a value that is not callable (usually, a non-function), you will see an error when the event triggers. | |||
Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the event generation code. | |||
Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the | |||
=Connections= | =Connections= | ||
Line 132: | Line 122: | ||
! Property !! Description | ! Property !! Description | ||
|- | |- | ||
| | | {{type|bool}} connected||If the connection is connected to an event, {{true}}. Otherwise, {{false}}. | ||
|} | |} |
Latest revision as of 04:33, 27 April 2023
A RBXScriptSignal, more commonly called an event, is a special kind of Roblox Object. It provides a way for user-defined functions to be called when something happens in the game. A RBXScriptSignal is similar to a property, but instead of holding a variable, events fire when a certain event happens. When a RBXScriptSignal fires, functions that are called "handlers", which are connected, or bound, to the event are ran.
Methods
Events have these methods:
Method | Description | Notes |
---|---|---|
connect(function handler) | Establishes a function to be called whenever the event occurs. Returns a RBXScriptConnection object. | |
connectFirst(function handler) | Same as connect except handler functions gets called before any other handler. | Protected and Deprecated |
connectLast(function handler) | Same as connect except handler functions gets called after all the other handlers. | Protected and Deprecated |
wait() | Pauses the script until the event is fired and returns any arguments the event returns. | |
disconnect() | Can not be used anymore; prints an error message. Use RBXScriptConnection:disconnect() instead. | Obsolete |
Usage
Take the example of the Touched event of the Part instance:
Touched ( BasePart otherPart ) | |
Description | Fired when another object comes in contact with this object. |
---|---|
Member of: | BasePart |
The top of the event box tells you that it is an event named "Touched" which has arguments "BasePart otherPart". The middle section explains when the event will fire and why. The last section lists the objects that have this event.
We use this information on the event to create a connection to an event line in our script.
Functions that are called when an event fire are called handlers.
The arguments that will be passed by the event are given by the name of the event. In this case it is "BasePart otherPart". This means that otherPart will be passed and the type of otherPart will be a BasePart (which all Parts are).
To allow our function to use that, we defined it as:
function onTouched(otherPart)
--code
end
Finally, we can use the connection method of events to bind our function to the event.
part.Touched:connect(onTouched)
Here's an example using the Touched event of a Part object.
local part = game.Workspace.Part -- your part here
function onTouched(otherPart)
otherPart.Transparency = 1 -- make part that was touched invisible
part.BrickColor = BrickColor.Random() -- give this part a new random color
end
part.Touched:connect(onTouched) -- call onTouched each time something touches
Here we are giving otherPart as an argument to the event handler, onTouched. When part is touched, the part that touches part will turn invisible and part will become a different color.
Advanced Usage
Any function name is valid:
function blah()
print("In event")
end
part.Changed:connect(blah)
Using anonymous functions in connect is common and results in slightly shorter code. This can be useful if you only need to connect a function to an event once.
part.Touched:connect(function(otherPart)
otherPart.Transparency = 1
part.BrickColor = BrickColor.Random()
end) -- ends the anonymous function and the ''connect'' method call.
You can also use anonymous functions to change the arguments:
player.Chatted:connect(function(message, recipient)
onChatted(player, message) -- new arguments need to match onChatted function call
end)
Connections can be created at any time, even inside of event functions:
function onChatted(player, message)
print(player .. " said " .. message)
end
function onPlayerAdded(player)
player.Chatted:connect(function(message, recipient)
onChatted(player, message)
end)
end
game.Players.PlayerAdded:connect(onPlayerAdded)
Notes
Connections are automatically disconnected if:
- The event handler generates an error before the first wait() call.
- The script itself is removed or reparented.
- The object the event relates to is destroyed with the Destroy method.
Note: The connect method does not check that the argument is a function/is callable. If you give a value that is not callable (usually, a non-function), you will see an error when the event triggers.
Notice that the error has no script reference or line number. This is because it comes from Roblox itself inside the event generation code.
Connections
The connection object is a special object returned by the connect methods of an Event.
Methods
Connections have one method:
Method | Description |
---|---|
disconnect() | Disconnects the connection from the event. |
Properties
Connections have one property:
Property | Description |
---|---|
bool connected | If the connection is connected to an event, true. Otherwise, false. |