Resetting player camera after using Run Service/ Render Step?

I am trying to create a 2D/ normal camera toggle. I do not normally work with the camera other than tweening it so sorry for my lack of knowledge here. Below is my current script, set2D function works fine but setDefault does seeming nothing… any solutions?

rs = game:GetService('RunService')
cam = workspace.CurrentCamera
original_CF = cam.CFrame
toggle = workspace["Interactions/ Assets"].Toggles["2DCam"]
original_CF = cam.CFrame

toggle.Changed:Connect(function()
	if toggle.Value == true then
		if p.Team ~= game.Teams.Observers and p.Team ~= game.Teams.Trainers and p.Team ~= game.Teams.TA then
			game:GetService("RunService").RenderStepped:connect(set2D)
		end
	else
		game:GetService("RunService").RenderStepped:connect(setDefault)
	end		
end)

function set2D()
	if p.Character then
		local torso = p.Character:WaitForChild("Torso")
		cam.CFrame = CFrame.new(
			torso.CFrame.X,
			torso.CFrame.Y + 2.4,
			24
		)
	end
end

function setDefault()
	if p.Character then
		local torso = p.Character:WaitForChild("Torso")
		cam.CFrame = CFrame.new(
			torso.CFrame.X,
			torso.CFrame.Y,
			0
		)
	end
end

This isn’t optimized code at all. Use BindToRenderStep and UnbindFromRenderStep.


rs = game:GetService('RunService')
cam = workspace.CurrentCamera
original_CF = cam.CFrame
toggle = workspace["Interactions/ Assets"].Toggles["2DCam"]
original_CF = cam.CFrame

toggle.Changed:Connect(function()
	if toggle.Value then
		if p.Team ~= game.Teams.Observers and p.Team ~= game.Teams.Trainers and p.Team ~= game.Teams.TA then
			 rs:BindToRenderStep("Spectate", Enum.RenderPriority.Camera.Value, set2D)
		end
	else
		pcall(rs.UnbindFromRenderStep, rs, "Spectate")
	end		
end)

function set2D()
	if p.Character then
		local torso = p.Character:WaitForChild("Torso")
		cam.CFrame = CFrame.new(
			torso.CFrame.X,
			torso.CFrame.Y + 2.4,
			24
		)
	end
end
1 Like