Do RunServices automatically disconnect?

I’m trying to familiarise myself more with coding, hence this question.

I have a Spaceship flight system where upon entering the Pilot Seat, the Ships Ownership is set to the Pilot and a Gui is inserted into the Player - allowing the Ship to be piloted via client.

This uses RunServices to change the Camera focus to the Starship, although upon exiting the seat - This GUI is destroyed.

General query, but is the Function destroyed when the GUI is as at that times its not in use?

The event itself should not be disconnected unless the Lua thread is garbage collected. So it depends on if you have weak references or not.

I wouldn’t rely on this and you should be cleaning up your connections for sanity.

local rs: (RunService) = game:GetService("RunService")
local connection: (RBXScriptSignal) = rs.RenderStepped:Connect(function(dt: (number))

end)

wait(1)
connection:Disconnect()
1 Like

How would I rig it to disconnect if the Player is no longer on the Pilot Seat, needing it to be Disconnected?

No it is not destroyed, you have to Disconnect it as it says @MrNicNac
Only I’m not a fan of its code, even if it works fine so this is a another exemple

local RunService = game:GetService("RunService")
function onStep()
print("New step !")
end

local idkForName = RunService.Stepped:Connect(onStep) -- Connect the service to the function onStep
wait(5)
idkForName:Disconnect() -- Disconnect the service

Well, it depends on what’s going on in the code. You can detect if the player have leaving the seat with VehicleSeat/Occupant

1 Like