Does anyone know what :Connect() is capable of?

Here’s an example of what you can do with :Connect()

-- Connect a function to an event
game.Players.PlayerAdded:Connect(function(Player)
end)

But is that all :Connect() is capable of doing? I never really bothered to try to think outside the box with scripting but this makes me curious.

1 Like

You can use it with any event, such as humanoid dying, object properties changing, player joining a team, etc

1 Like

You can call :Once which is similar to :Connect but only runs once (hence the name), :ConnectParallel which runs in parallel, or :Wait which yields until the event fires. You can also pass predefined functions through connections like this:

local function DoThing()
    -- Do thing
end
RunService.Heartbeat:Connect(DoThing)

Connections automatically pass arguments through the callback function, for instance:

local function OnHit(hitPart)
    print(hitPart) -- hitPart is passed through the connection
end
part.Touched:Connect(OnHit)

Connections take up memory, so make sure you call :Disconnect() on connections that are no longer needed! As an example, if you only need a .Heartbeat connection to run for x seconds, it would look like this:

local heartbeatConnection
local connectionStart = os.clock()
local x = 3 -- How long connection runs in seconds

heartbeatConnection = RunService.Heartbeat:Connect(function()
    local timeElapsed = os.clock() - connectionStart
    if timeElapsed > x then
        heartbeatConnection:Disconnect()
    end
end)

That’s all I can think of. Hope that helps!

1 Like

It would be cool if there were more uses for :Connect other than connecting a function callback

What else would you need to pass through a connection? Functions cover just about everything.

1 Like