Are multiple connections with the same function supposed to be made?

So I ran this code:

local function fn()
    print("Brick color changed")
end

for i = 1, 5 do
    workspace.Baseplate:GetPropertyChangedSignal("BrickColor"):Connect(fn)
end

workspace.Baseplate.BrickColor = BrickColor.new("Really red")

And it printed Brick color changed five times.

I expected future calls to :Connect with the same function to just not do anything or even return the same connection.

So is this supposed to happen? Not that this is a problem for me, but I just find it unnecessary for multiple connections to be made, when the same function is being used.

Yes, this is intended behaviour. Connect creates a new special object, RBXScriptConnection. Your listener function fn is essentially held in this object and called whenever the event is fired.

I do believe it’d be fairly counterintuitive if Connect ignored same-function connections or returned the same connection, since there’s no reason for it to be memoised. There’s also the case of disconnecting an RBXScriptConnection; if you still have a reference, it still exists, so trying to connect would just give you back a disconnected connection if this was the behaviour.

1 Like