local RunService = game:GetService("RunService")
local renderStepped
renderStepped = RunService.RenderStepped:Connect(function()
-- Execute some code
renderStepped:Disconnect()
end)
This code works fine, but is very messy and spans quite a few lines. Is there any way this can be cleaned up?
Try adding a bool value for the renderStepped. If it’s enabled, it’ll run. But if it’s disabled, it would stop the script entirely because it was expecting the bool value to be true.
local RunService = game:GetService("RunService")
local ableToRun = true
local renderStepped
renderStepped = RunService.RenderStepped:Connect(function()
if ableToRun then
-- Execute some code
end
end)
Then wherever another function would make ableToRun = false, it’ll shut this code off.
And because the function is actually on a loop since it’s getting every frame possible on the clients side, it’ll be able to check if the value is true or false in a loop.
I believe this can help you. It binds a function to be called at a specific times during the render step. It also makes the coding cleaner and can stop renderstepped loops.
local RunService = game:GetService("RunService")
local function func()
-- Do stuff
RunService:UnbindFromRenderStep("Function1", Enum.RenderPriority.First.Value, func)
end
RunService:BindToRenderStep("Function1", Enum.RenderPriority.First.Value, func)
Why would you just connect .RenderStepped and then disconnect it? Can’t you just use while game:GetService("RunService").RenderStepped:Wait() do instead?