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)