Need help disconnecting this script

Hey! I hope you are having a great day! Currently, I am trying to disconnect a RBXScriptConnection, but when I try this code:

game.ReplicatedStorage.LocalPlayerEvents.EndFixedCamera.Event:Connect(function()
	if GoingInsideCamera ~= nil then
		GoingInsideCamera:Disconnect()
	end
	
	if OutdoorCamera ~= nil then
		OutdoorCamera:Disconnect()
	end
	
	GoingInsideCamera = nil
	OutdoorCamera = nil
end)

It doesn’t seem to work. Is this not how to specify connections?

If you can help, please let me know. Thanks! WE

Edit: There are no errors, and the variables are RunService RenderStepped.

1 Like

What are the variables GoingInsideCamera and OutdoorCamera? (Can you show them in the hierarchy). As well, I assume (please confirm), “EndFixedCamera” is a bindableEvent?

No, they are RunService RenderStepped.

GoingInsideCamera = RunService.RenderStepped:Connect(function()
	Camera.CFrame = CameraCFrame
end)

Edit: Mis-read. Yes it is a BindableEvent.

Rather than do that, you should use the function RunService:BindToRenderStep and RunService:UnbindFromRenderStep.

Hmm… Didn’t seem to work.

game.ReplicatedStorage.LocalPlayerEvents.EndFixedCamera.Event:Connect(function()
	if GoingInsideCamera ~= nil then
		--GoingInsideCamera:Disconnect()
		RunService:UnbindFromRenderStep("GoingInsideCamera")
	end

	if OutdoorCamera ~= nil then
		--OutdoorCamera:Disconnect()
		RunService:UnbindFromRenderStep("OutdoorCamera")
	end

	GoingInsideCamera = nil
	OutdoorCamera = nil
end)

What do the Binding functions look like now? They should look like this:

RunService:BindToRenderStep("GoingInsideCamera", 101, function()
	--Whatever code you want to run here
end))

Mine look like this, which should work also:

local function OutdoorCamera()
	local PlayerPosition = Character:WaitForChild("HumanoidRootPart").Position
	local CameraPosition = PlayerPosition + offset
	Camera.CoordinateFrame = CFrame.new(CameraPosition, PlayerPosition)
end
OutdoorCamera = RunService:BindToRenderStep("OutdoorCamera", 1, OutdoorCamera)
local function GoInCamera()
			Camera.CFrame = CameraCFrame
		end

		GoingInsideCamera = RunService:BindToRenderStep("GoingInsideCamera", 1, GoInCamera)

Try without declaring the return as a variable, there is no reason to do this anyways since the function returns void.

1 Like

Screen Shot 2021-05-25 at 12.37.55 AM

The prints I added printed, but it is still not working. It also seems that the camera is still my custom one…

The entire script
Script deleted


It seems the camera not returning to it’s normal state is a completely different problem, try adding prints inside of the Functions being run on RenderStep to see if it stops printing when you unbind the function

I am now using 1 script instead of 2, and it seems to be working fine now!