RunService:BindToRenderStep only when a boolean is true

I have a RunService:BindToRenderStep that I only want to run when a boolean is true. How can I achieve that? Thanks!

local RunService = game:GetService("RunService")

local function renderPreview()
	print("Running")
end

RunService:BindToRenderStep("Preview", Enum.RenderPriority.Camera.Value, renderPreview)
1 Like

Not sure what you mean, need more context.

local function renderPreview()
	if state then
	    --do code
    else
        --do other code
    end
end
1 Like

Something like this should work to achieve what you’re looking for (condition is the boolean value to change):

local RunService = game:GetService("RunService")

local active = false

local condition = true

local function renderPreview()
	print("Running")
end

RunService.Heartbeat:Connect(function()
	if condition then
		if active then return end -- This is to prevent calling BindToRenderStep multiple times unnecessarily
		active = true

		RunService:BindToRenderStep("Preview", Enum.RenderPriority.Camera.Value, renderPreview)
	else
		if active then
			active = false

			RunService:UnbindFromRenderStep("Preview")
		end
	end
end)

@Guest_2000123145 Alternatively, if condition is a BoolValue it makes things much simpler:

local RunService = game:GetService("RunService")

local condition = -- Where the BoolValue is located

local function renderPreview()
	print("Running")
end

condition.Changed:Connect(function(value)
	if value then
		RunService:BindToRenderStep("Preview", Enum.RenderPriority.Camera.Value, renderPreview)
	else
		RunService:UnbindFromRenderStep("Preview")
	end
end)

yep, the second option is perfect! Thank you :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.