Skip to content

Event

Event is a data type that represents listenable events.

You aren't able to create your own listenable events using this data type.

Events are present on many different objects and are marked with on the Documentation.

Methods

Connect → void

Parameters: function

Connect the specified function to the event. When the event is fired, the function will be ran. The event parameters may be passed to the function, if there are any.

Example

The function may be specified in 2 different ways:

function hitListener(hit)
    print(hit.Name .. "touched the Part!")
end

game["Environment"]["Part"].Touched:Connect(hitListener)

and

game["Environment"]["Part"].Touched:Connect(function(hit)
    print(hit.Name .. "touched the Part!")
end)

Note: Defining a function within the Connect method will cause the function to be non-removable from the event.

Disconnect → void

Parameters: function

Disconnect the specified function from the event.

Example

function hitListener(hit)
    print(hit.Name .. "touched the Part!")
end

game["Environment"]["Part"].Touched:Connect(hitListener)
wait(5)
-- hitListener(hit) will no longer run when the event is fired after the following line
game["Environment"]["Part"].Touched:Disconnect(hitListener)