Temporairly disconnect connection?

I have a RenderStepped loop in my code, and I was wondering if I can temporairly disconnect it when its not needed.

The use case right now is for an inventory menu, the items in ViewportFrames spin with RenderStepped.
I wanted to disconnect that connection when you are not in the inventory menu, since you dont need it.
I already added an if statement to check if the Gui is enabled, but the script per second rate is still high

Can a RenderStepped loop that does nothing still cause lag issues?

local runService = game:GetService("RunService")

local connection = runService.RenderStepped:Connect(function()
    
end)

connection:Disconnect() -- This will disconnect the function

Yes but how would I connect it back?

You could make it a function
Local connection
Function handleConnection()
Connection = runService.RenderStepped:Connect(function()
End)
End

HandleConnection()
Connection:Disconnect

Sorry for sloppy code on mobile rn

local runService = game:GetService("RunService")

local connection

local function rs()
    -- RenderStepped function
end

local function guiOpen()
    if (not connection) then
        connection = runService.RenderStepped:Connect(rs)
    end
end

local function guiClose()
    if (connection) then
        connection:Disconnect()
    end
end
2 Likes

One more thing, if I need dt in the RenderStepped loop, how would I get it in this configuration?
Here is what I mean:

game:GetService("RunService").RenderStepped:Connect(function(dt)
	angle = angle + 1 * dt * 60
end)
local function rs(delta) -- the first parameter is dt
    print(delta)
end

runService.RenderStepped:Connect(rs)
1 Like

Oh, thats good to know! Thank you!