How do i end a function within itself?

For example, if i did RunService.Heartbeat:Connect(function() end), how would i disconnect the event within itself? Because doing something like local heartbeat = RunService.Heartbeat:Connect(function() heartbeat:Disconnect() end) doesn’t work.

You need to either define the variable before instantiating it, or make it a global variable (but is bad if multiple functions can be called at once)

local heartbeat
heartbeat = RunService.Heartbeat:Connect(function()
    heartbeat:Disconnect()
end)

The first one worked just fine! Thank you for your help.