Why do people do events like this?

I want to know why people Connect to Events like this, I couldn’t find anything on it, and nobody really mentions it, so I thought I’d make a post asking why.

Example
function touched(hit)
print(hit.Name)
end

script.Parent.Touched:Connect(touched)

This is the default way of connecting events and is seen throught roblox-wiki styled code. It simply runs the function when the event is fired. The main advantage of using this over simply

Event:Connect(function()
    
end)

Is you can connect multiple events to the same function

So it can detect when the player interacts with different parts of the game.

This is essentially the same as writing;

Part.Touched:Connect(function()

end)

What you see in this example is basically the same as what you have in your example, the exception being instead of creating a new function solely for this connection, that we use a preset in your example instead. In the example I’ve listed, we’re just tying a new function instead of an already created function, this is fairly common, both methods are practical.

No, I know what Events do, I’m asking why people do this because I usually do it like this:

script.Parent.Touched:Connect(function(hit)
print(hit.Name)
end)

They usually do it so they can use the same function multiple times.

It allows for re-usability. Say you have multiple buttons that you want to change a textLabel with. Instead of writing 5 different functions that do the same thing for 5 buttons, you can write one function with similar functionality and pass in a different parameter for each button.

Oh I see, thanks for explaining it to me.

1 Like

I would NEVER make an event like this:

game.Workspace.Part.Touched:Connect(function()
     --nothing
end)

As it has a function i do not like. Plus its easier to use this:

function event()
   --nothing
end

game.Workspace.Touched:Connect(event)

Some might say it can be a bit cleaner to connect it to a function. Since when you create a script structure it looks a bit cleaner like this. This makes it a bit more easier to see the events a script is using
image

Also performance.

local door = workspace.Door
local connection
local ActivateOpen = Instance.new("BindableEvent")
ActivateOpen.Parent = script

local function CloseDoor()
   connection:Disconnect()
   print("Door closing...")
   task.wait(1)
   ActivateOpen:Fire()
end

local function OpenDoor()
   connection:Disconnect()
   print("Door opening...")
   task.wait(1)
   connection = door.Touched:Connect(CloseDoor)
end

local function OnActivateOpenEvent()
   connection = door.Touched:Connect(OpenDoor)
end
ActivateOpen.Event:Connect(OnActivateOpenEvent)

--initial setup
connection = door.Touched:Connect(OpenDoor)

This may seem convoluted, but it will prevent multiple function executions running. Adding debounce or checks will never be that efficient.

Edit:
There are however some benefits to using inline function declaration. For example:

for _, part in pairs(SomeFolder:GetChildren()) do
   part.Touched:Connect(function(hit)
      print(part.Name .. " has been hit. We would not be able to figure out that easily by using a named function")
   end)
end

Notice how you still have access to the part that you are connecting the function. True, it can be done without this trick, but not as easy as this.