RBXScriptConnection

RBXScriptConnection

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

Functions
SyntaxDescription
RBXScriptConnection:Disconnect()Disconnects the connection from the event.
RBXScriptConnection:disconnect()Disconnects the connection from the event.
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.

Methods

Disconnect

void RBXScriptConnection:Disconnect()

Description:
Disconnects the connection from the event.

disconnect

void RBXScriptConnection:disconnect()

Description:
Disconnects the connection from the event.

Properties

Connected

bool RBXScriptConnection.Connected [readonly]

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

connected

bool RBXScriptConnection.connected [readonly]

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

Usage

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)