Events
In addition to properties and functions, every object also has events which can be used to set up cause-and-effect systems. Events send out signals when specific things happen in a game, such as a player touching an object or a player connecting to the game. To fire an event is to have it send out such a signal.

Waiting for an Event

The Wait() function will cause the script to pause until the event occurs once. When it does, the function returns the data associated with the event’s firing.
1
local myPart = game.Workspace.Part
2
3
-- Wait until another part collides with "myPart"
4
local otherPart = myPart.Touched:Wait()
5
print("Part was touched by: " .. otherPart.Name)
Copied!

Connecting a Function

The Connect() function can be used when a given function should run every time an event fires. This function immediately returns a connection object. In the example below, we connect a function, onTouched(), to a Part in the Workspace. If another part collides with myPart, the script prints the name of the other part involved in the collision.
1
local myPart = game.Workspace.Part
2
3
local function onTouched(otherPart)
4
print("Part was touched by: " .. otherPart.Name)
5
end
6
myPart.Touched:Connect(onTouched)
Copied!

Disconnecting a Function

Eventually, you may no longer need a connected function to run when an event fires. To disconnect it, use the Disconnect() method of the connection object returned by Connect().
1
local points = Instance.new("NumberValue")
2
points.Name = "Points"
3
points.Value = 0
4
5
local connection
6
7
local function onPointsChanged(newPoints)
8
print("Points: " .. newPoints)
9
if newPoints >= 50 then
10
-- Stop listening for changes if we have at least 50 points
11
connection:Disconnect()
12
end
13
end
14
15
connection = points.Changed:Connect(onPointsChanged)
16
17
-- Trigger some changes
18
points.Value = 25
19
points.Value = 100
20
points.Value = 0 -- Prints nothing because we called Disconnect()
Copied!

Event Data

Almost every event in Roblox will send data relevant to the event’s occurrence. For example, when a Player joins the game, the Players.PlayerAdded event fires with a reference to the new player.
1
local Players = game:GetService("Players")
2
3
local function onPlayerAdded(player)
4
print(player.Name .. " joined the game")
5
end
6
7
Players.PlayerAdded:Connect(onPlayerAdded)
Copied!

Nested Connections

Sometimes you will need to connect a function to an event on an object provided by another event. To do so, define a second local function within the function connected to the first event.
A common example is detecting when a player’s character is spawned into the game. For this, you’ll need to access the CharacterAdded event of the Player involved in the Players.PlayerAdded event.
1
local Players = game:GetService("Players")
2
3
local function onPlayerAdded(player)
4
local function onCharacterAdded(character)
5
print(player.Name .. " spawned in: " .. character:GetFullName())
6
end
7
player.CharacterAdded:Connect(onCharacterAdded)
8
end
9
10
Players.PlayerAdded:Connect(onPlayerAdded)
Copied!
1
local part = script.Parent
2
3
4
local function onPartTouched(otherPart)
5
local partParent = otherPart.Parent
6
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
7
print(humanoid.Health)
8
if humanoid then
9
game.Workspace.Cylinder.BrickColor = BrickColor.new("Bright green")
10
humanoid.Health = humanoid.Health - 10
11
print(humanoid.Health)
12
print("Chaya was here")
13
end
14
end
15
16
part.Touched:Connect(onPartTouched)
Copied!
Last modified 4mo ago