Hello! Is there a way to completely disable events unless a GUI option is enabled? I have a couple of stepped events constantly running even when they’re not needed taking up resources.
i don’t think (I’m not sure) because a evenement is a evenement.
Idk but for context I’m making a GUI with a bunch of events that I just don’t want running unless the GUI option is enabled for that function. Stepped event runs every frame and I’m using maybe 10 of them probably more in the future to connect functions. Most of the functions are unused and toggled with a variable inside of them.
local function featureName()
if variable == true then
run code
end
whatever.Stepped:Connect(featureName)
whatever.Stepped:Connect(featureName1)
whatever.Stepped:Connect(featureName2)
whatever.Stepped:Connect(featureName3)
whatever.Stepped:Connect(featureName4)
whatever.Stepped:Connect(featureName5)
whatever.Stepped:Connect(featureNameeEtc)
Just comment them out, like this
local function featureName()
if variable == true then
run code
end
whatever.Stepped:Connect(featureName)
-- whatever.Stepped:Connect(featureName1)
-- whatever.Stepped:Connect(featureName2)
-- whatever.Stepped:Connect(featureName3)
-- whatever.Stepped:Connect(featureName4)
-- whatever.Stepped:Connect(featureName5)
-- whatever.Stepped:Connect(featureNameeEtc)
You can un-comment them out whenever you add the functions later.
You can also run an if statement on the Stepped event to check if the option is enabled, for example:
local runService = game:GetService("RunService")
local gui -- whatever gui you want
local function featureName()
if variable == true then
run code
end
runService.Stepped:Connect(function()
if gui.Enabled == true then -- checks if the gui is enabled then runs the functions, you can change this property to anything
featureName()
end
end)
Yes there is a way, and I often use Janitors to help me with cleaning up events and connections.
Example:
local janitor = Janitor.new()
-- toggleRenderSteppedConnection is used here as an example of a Signal or Bindable Event
toggleRenderSteppedConnection.Event:Connect(function(toggle: boolean)
if toggle then
-- Set all RenderStepped connections here
janitor:Add(RunService.RenderStepped:Connect(function(deltaTime)
-- Code happens in here
end))
else
-- Cleans up connections, hence 'disabling' them
janitor:Cleanup()
end
end
It is also recommended not to have that many RenderStepped
connections. Is there a reason why there are so many?
RBXScriptSignal | Roblox Creator Documentation (relating to event signals)
What is the :Disconnect() function primarily used for? - #2 by incapaz (credit to this user)
local connection
connection = script.Parent.Touched:Connect(function(part)
if something then
connection:Disconnect()
end
end)
I think you just have to define a variable outside your event, then set that variable equal to your event on a separate line
from there, you can just disconnect your event by doing
variable:Disconnect()
Exactly what I was looking for. Thanks much! I’ve seen these in scripts before and couldn’t figure out what their use was. Appreciate the documentation link as well