: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:
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.