This concept may be a little obscure since some developers aren’t knowledgeable about this datatype.
An event, which is a common term for the datatype RBXScriptSignal, refers to a special kind of Roblox object. The functions you attach via the Connect method are called listeners. These allow you to run certain actions when something happens in your game.
There are two ways to connect a listener to an event. These are done through the signal’s Connect method, something you should be very familiar with working with.
-- A
local function onTouched(hit)
print(hit)
end
Part.Touched:Connect(onTouched)
-- B
Part.Touched:Connect(function (hit)
print(hit)
end)
There are only two real differences between either of these methods and what you choose to use will depend on the context and necessities of your code.
The first example is if you’re looking for something readable and reusable. By defining a function to a variable, you can reuse this function in various ways. An example of where reusability may come handy is when you need to account for existing and new players in a data handler.
local Players = game:GetService("Players")
local function onPlayerAdded(player)
-- Do something to player
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _, player in ipairs(Players:GetPlayers()) do
onPlayerAdded(player)
end
The second example comes in handy if you’re looking for a quick, accessible way to connect a listener to a signal when you have a function only relevant to that connection and it doesn’t need to be used elsewhere. This is a common way to write out functions and it is perfectly fine to do.
Signals are fired internally, so you as the developer are only able to connect to them with your listener functions. There is, however, an exception if you create a custom signal class with ModuleScripts or use BindableEvents, which allow you to make your own events to both connect to and fire.
Some signals will fire with some data values called arguments. Your listener functions will receive these as parameters. Have you ever written a function where you put something in the brackets? These are parameters. Some events will pass data through which your listener catches and works with. For example, the Touched event for a Part will fire a touched part to the signal and your listener function will receive which other part touched the current one.
-- Your listener
Part.Touched:Connect(function (Hit)
-- The signal internally
Part.Touched:Fire(touchedPart)
You can also, as mentioned earlier, achieve this behaviour using BindableEvents. You, as the developer, are able to fire off these signals with some data values. Think of this as indirectly calling your function with a couple of values. Your listener functions will receive this data and then run themselves with the data as usable variables.
This should set you good for events. To wrap things up, let’s review the information. Functions (actually listeners) and events (actually signals) are not one in the same, but they work together to achieve game functionality. You connect a function to an event and that function listens for the event to fire. This allows you to run actions when certain things in your game happen. Events for instances are fired internally by the engine and you can create your own using BindableEvents.