Roblox Wiki
Advertisement
Roblox Wiki
For other uses, see Event (disambiguation).

In Roblox programming, an event represents an action or occurrence within an experience. Events are fundamental because they allow scripts to respond to user inputs, physics and other systems.

A class can have events which are represented as RBXScriptSignals. An RBXScriptSignal can be connected to a function by calling the :Connect() method with the function being passed. The function known as an event handler is called every time the event fires. The method returns an RBXScriptConnection, which allows the connection to be disconnected by calling the :Disconnect() method. The event can also pass arguments to the function. Additionally, an RBXScriptSignal can be connected to a function that can only called once by calling :Once(), which returns a connection that will disconnect when the event is fired. A thread can also wait for an event to fire by calling the :Wait() method. This method will return the event's arguments as when a connection passes them to the event handler. Examples of events are: when a Part light iconPart dark iconPart is being touched (Part.Touched) or a Player light iconPlayer dark iconPlayer respawning (Player.CharacterAdded).

Examples[]

This code creates a Part light iconPart dark iconPart in the Workspace and connects its Touched event to a function that will print a message every time the part is touched:

local part = Instance.new("Part")
part.Parent = workspace

local connection = part.Touched:Connect(function(hit)
    print("I've been touched by " .. hit.Name)
end)

When another part touches, the event passes the touching Part light iconPart dark iconPart as an argument "hit" to the function, in which it will print out the touching part's name. The "connection" variable returned by calling :Connect() is a RBXScriptConnection.

Advertisement