How to Disconnect RenderStepped inside Function (Read more)

So, I have this ViewPortFrame to show object and the Camera it contains spins using RunService (RenderStepped). I want it to disconnect as soon as new function will call from the same function.

local isRendering = false
local Rotation = 0
function module.UpdateCharacterModelPreview(CharacterName, OutfitNumber, PreviewImageFrame, CharactersFolder)
	
	local RenderStepped
	
	local PreviewCamera = Instance.new("Camera", script)
	PreviewCamera.Name = "PreviewCamera"
	PreviewCamera.CameraType = Enum.CameraType.Scriptable
	
	local PreviewSubject
	
	local ViewportPoint = Vector3.new(0,0,0)
	
	local ViewportPreview = PreviewImageFrame:WaitForChild("ViewportPreview")
	
	--// Delete the current preview subject
	for _, content in pairs(ViewportPreview:GetChildren()) do
		if not content:IsA("UICorner") then
			content:Destroy()
		end
	end
	
	--// Subject
	local function findSubject()
		for _, CharFolder in pairs(CharactersFolder:GetDescendants()) do
			if string.match(string.lower(CharacterName), string.lower(CharFolder.Name)) and CharFolder:IsA("Folder") then
				print(CharacterName, "found!")
				return CharFolder
			end
		end
	end
	
	--// Get outfit number
	local function getCharTable()
		for _, CharTable in pairs(Characters) do
			if string.match(CharacterName, CharTable["Name"]) then
				return CharTable
			end
		end
	end
	
	local OutfitName = getCharTable()["Context"]["Outfits"][OutfitNumber]
	
	local SubjectFolder = findSubject()
	for _, children in pairs(SubjectFolder:GetChildren()) do
		if string.match(OutfitName, children.Name) then
			local newChildren = children:Clone()
			PreviewSubject = newChildren
			break
		end
	end
	
	ViewportPreview.CurrentCamera = PreviewCamera
	PreviewSubject:SetPrimaryPartCFrame(CFrame.new(ViewportPoint))
	PreviewSubject.Parent = ViewportPreview
	
	local cframe, size = PreviewSubject:GetBoundingBox()
	local Max = math.max(size.X, size.Y, size.Z)
	local Distance = (Max/math.tan(math.rad(PreviewCamera.FieldOfView))) * 1.5
	local CurrentDistance = (Max/2) + Distance
	
	RenderStepped = game:GetService("RunService").RenderStepped:Connect(function()	
		PreviewCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0,0,CurrentDistance), ViewportPoint)
		Rotation += 1
		
		isRendering = true
	end)
	
	
end 

I’ve tried putting RenderStepped:Disconnect() from the top but the output says error because nil.

but when I put it below the RenderStepped it will stop the RunService.

Whats the best way to disconnect a RunService here?

It’s nil because script always start at the First Line. till it reach the bottom

Just my suggestion if you’re trying to disconnect it when rendering is false

local isRendering = true
local RenderStepped
RenderStepped = game:GetService("RunService").RenderStepped:Connect(function()	
if isRendering == false then RenderStepped:Disconnect() return end
		PreviewCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0,0,CurrentDistance), ViewportPoint)
		Rotation += 1
	end)
4 Likes

But considering what you want try doing it like this

local Signals = {}
local RS = game:GetService("RunService")

function Test(Player) -- lets use the player to identify which signal
	if Signals[Player] then
		Signals[Player]:Disconnect()
		Signals[Player] = nil
		return
	end
	
	Signals[Player] = RS.RenderStepped:Connect(function()	
		PreviewCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0,0,CurrentDistance), ViewportPoint)
		Rotation += 1
	end)
	
end

Test(game.Players.SomeGuy)
1 Like

This is because the RenderStepped variable is in the scope of the function, when you call it again, the variable no longer exists within that context i.e.

image

Put the ‘RenderStepped’ variable outside of the function, then within the function, check if the variable exists and if so, disconnect it.

You could also use this

2 Likes

Try making a BindableEvent that fires when you want to disconnect. Outsite the function listen to BindableEvent.Event and when it fires disconnect the function.

Hope this works

Thanks for the help @DevSynaptix and some of who suggest.

I am now using Bind and Unbind feature of RenderStepped. and its working as fine.