RBXScriptConnection

(Redirected from API:RBXScriptConnection)

RBXScriptConnection[edit]

The RBXScriptConnection, also known as a Connection, is a special object returned by the Connect method of an Event. This is used primarily to disconnect a listener from an Event.

Quick Reference[edit]

Properties
SyntaxDescription
bool connected Indicates if this connection is still connected to its signal.
bool Connected Indicates if this connection is still connected to its signal.
Functions
SyntaxDescription
RBXScriptConnection:disconnect()Disconnects the connection from the event.
RBXScriptConnection:Disconnect()Disconnects the connection from the event.

Methods[edit]

Disconnect[edit]

void RBXScriptConnection:Disconnect()

Description:
Disconnects the connection from the event.

disconnect[edit]

void RBXScriptConnection:disconnect()

Description:
Disconnects the connection from the event.

Properties[edit]

Connected[edit]

bool RBXScriptConnection.Connected [readonly]

Description:
Indicates if this connection is still connected to its signal.

connected[edit]

bool RBXScriptConnection.connected [readonly]

Description:
Indicates if this connection is still connected to its signal.

Usage[edit]

This object is useful when you no longer want a listener to be connected to an Event.

When the Connect method is called, it returns a Connection. This can be used later on to disconnect the listener.

local part = Instance.new("Part")
 
local function onTouched(hitPart)
	print("I've run into a " .. hitPart.Name .. " brick!")
end
 
local touchedConnection = part.Touched:Connect(onTouched)
 
---- Somewhere else
touchedConnection:Disconnect()


You may have a case where you only want to listener to be called once. You might do that by disconnecting the connection from within the function.

local con
con = part.Touched:Connect(function(hit)
	con:Disconnect()
	print("Collided with " .. hit.Name)
end)