What is a variable connection?

Hello, fellow devs! So, I was learning scripting and I came across something named like connection? At some places I saw they are used like this:

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local connection
        
        connection = char:WaitForChild("Humanoid").Died:Connect(function()
            connection:Disconnect()
        end)
    end)
end)

Now I’m really confused how it works, so it would be really appreciated if anybody could explain this!

  • AridTheDev
1 Like

Assuming you’re talking about the variable connection and not :Connect > its just a variable. Heres a wiki link to learn more about them.

Variables (roblox.com)

2 Likes

:Connect() essentially binds functionality upon an event triggering. :Connect() has one parameter ‘function’, meaning when the event you use :Connect() on does happen, the function inside the parameter runs.

In the example that you have provided, :Connect() is bound to the ‘PlayerAdded’ and ‘CharacterAdded’ events, when the player is added into the game, the function inside the parameter will run:

:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        local connection
        
        connection = char:WaitForChild("Humanoid").Died:Connect(function()
            connection:Disconnect()
        end)
    end)
end)

‘plr’ is an instance of the player being added which is given from the ‘PlayerAdded’ event, hence in the function within the :Connect, ‘plr’ is there:

https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded

The exact same happens with the ‘CharacterAdded’ event, we use :Connect() and a function is provided in the parameter, you’ll notice that ‘char’ is within the function parameter this time, this is because CharacterAdded returns an instance of the character model:

https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

Essentially, when you use :Connect(), you’re connecting functionality to an event occuring. This page might also be of some use to you, it describes how events are handled along with providing more information as to how :Connect() and other functions are used:

https://developer.roblox.com/en-us/articles/events

As @Ryuunske said, ‘connection’ is a variable and not an actual Roblox function or object. But in the case that you have provided, ‘connection’ is set to a connected function to the ‘Died’ event which happens when the game recognises the player has died:

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

You’ll notice how that event doesn’t return anything to use for the function() parameter, not all events return anything to provide in the function() parameter, use the wiki to find out what events provide and what they don’t.

Within the :Connect() function now, you’ll notice this:

:Connect(function()
    connection:Disconnect()
end)

:Disconnect() is a function which only works on events/instances which already have a :Connect() bound to them. This is why in this case, the connection is set to a variable, so that the connection can be stored so the connection can be disconnected at any time.

:Disconnect() is essentially destroying the connection to the Died event, meaning:

connection = char:WaitForChild("Humanoid").Died:Connect(function()
    connection:Disconnect()
end)

This will no longer run the connected function when the Died event is called anymore, because we disconnected the connection.

If you have any questions or need something else explaining in more detail, don’t hesitate to ask. :smiley:

4 Likes

An addition to your post, only disconnect events when you really don’t need them anymore. However, somethings this is unmessacrfy.

3 Likes

Thank you for the addition! :smiley:

It depends on your use-case whether your code needs a connection disconnected. The main thing is, if you no longer want a function to be connected upon an event happening, you should use :Disconnect().

2 Likes

I assume this is more or less what you meant, but from a performance standpoint - you should be disconnecting every single connection that does not get reused in order to free up memory (I think).

2 Likes

I don’t agree with you, I believe the way how Roblox handles event will have like caches.

2 Likes

As specified in the Roblox Developer ‘Handling Events’ article:

Always call Disconnect() when a connection is no longer needed. Forgetting to do so can cause your game to use more resources than necessary. Note, however, that this may not always be necessary; when an object is destroyed, all connections to that object’s events are disconnected automatically.

https://developer.roblox.com/en-us/articles/events

From this, Roblox even advise you to utilise :Disconnect() as it uses more resources than is necessary if you were to leave a connection connected. But in the event that the instance is destroyed, the connection goes with it.

My view based on this information is, if a connection is no longer needed, disconnect the connection and if the instance a connection is bound to is going to be removed, just leave the connection connected unless you want it disconnected earlier.

So in the case of the .Died event with this logic, you wouldn’t need to use :Disconnect() on a Died event since the Humanoid instance gets destroyed anyway.

3 Likes

Thank you all for the help, and also sorry for the late response.

This would help a lot! Thanks to

  1. @Ryuunske,
  2. @xmthl,
  3. @ItzMeZeus_IGotHacked,
  4. @SummerEquinox!
2 Likes