Help with toggleable third person camera

I tried to make a toggleable third person camera by pressing a button, from a script I saw on YouTube:

local players = game:GetService("Players")
local context = game:GetService("ContextActionService")
local runservice = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local camera = workspace.CurrentCamera
local player = players.LocalPlayer

local offset = Vector3.new(2, 2, 8)

local enabled = false
local connection


player.CharacterAdded:Connect(function(character)
	local hum = character:WaitForChild("Humanoid")
	local root = character:WaitForChild("HumanoidRootPart")
	
	player.CameraMode = Enum.CameraMode.LockFirstPerson
	camera.CameraType = Enum.CameraType.Custom
	uis.MouseBehavior = Enum.MouseBehavior.Default
	hum.AutoRotate = true
	
	local startcamcf = camera.CFrame
	
	local anglex = 0
	local angley = 0
	
	local function playerinput(action, state, obj)
		if state == Enum.UserInputState.Change then
			anglex -= obj.Delta.X
			angley = math.clamp(angley - obj.Delta.Y * 0.4, -75, 75)
		end
	end
	
	connection = uis.InputBegan:Connect(function(input, processed)
		if processed then
			return
		elseif not processed then
			if input.UserInputType == Enum.UserInputType.Keyboard then
				local key = input.KeyCode
				if key == Enum.KeyCode.H then
					if enabled == true then
						enabled = false
						
						camera.CameraType = Enum.CameraType.Custom
						player.CameraMode = Enum.CameraMode.LockFirstPerson
						player.CameraMinZoomDistance = 0.5
						uis.MouseBehavior = Enum.MouseBehavior.Default
						camera.CameraSubject = hum
						hum.AutoRotate = true
						uis.MouseIconEnabled = true
						camera.CFrame = startcamcf
					elseif enabled == false then
						enabled = true
						
						camera.CameraType = Enum.CameraType.Scriptable
						player.CameraMode = Enum.CameraMode.Classic
						player.CameraMinZoomDistance = 10
						uis.MouseBehavior = Enum.MouseBehavior.LockCenter
						camera.CameraSubject = hum
						hum.AutoRotate = false
						uis.MouseIconEnabled = false
						
						context:BindAction("PlayerInput", playerinput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
						
						runservice:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
							if enabled == true then
								local startcf = CFrame.new(root.CFrame.Position) * CFrame.Angles(0, math.rad(anglex), 0) * CFrame.Angles(math.rad(angley), 0, 0)
								local cameracf = startcf:PointToWorldSpace(offset)
								local camfocus = startcf:PointToWorldSpace(Vector3.new(offset.X, offset.Y, -10000))

								camera.CFrame = CFrame.lookAt(cameracf, camfocus)

								local looking = CFrame.lookAt(root.Position, camera.CFrame:PointToWorldSpace(Vector3.new( 0, 0, -10000)))

								root.CFrame = CFrame.fromMatrix(root.Position, looking.XVector, root.CFrame.YVector)
							elseif enabled == false then
								runservice:UnbindFromRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value)
								
								camera.CameraType = Enum.CameraType.Custom
								player.CameraMode = Enum.CameraMode.LockFirstPerson
								player.CameraMinZoomDistance = 0.5
								uis.MouseBehavior = Enum.MouseBehavior.Default
								camera.CameraSubject = hum
								hum.AutoRotate = true
								uis.MouseIconEnabled = true
								camera.CFrame = startcamcf
							end
						end)
					end
				end
			end
		end
	end)
	
	uis.InputEnded:Connect(function(input, processed)
		if connection then
			connection:Disconnect()
		end
		connection = nil
	end)
end)

local function focus(action, state, obj)
	if state == Enum.UserInputState.Begin and enabled == true then
		context:UnbindAction("FocusControl")
	end
end

context:BindAction("FocusControl", focus, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

But when you press the said button the camera stops following the player and that’s the thing I need help with. And if there is any other way of doing a toggleable third person camera, plz tell.

Do you mean toggle between first person and third person or from behind the player to behind and to the side.

First - Third person <<<<

I would suggest doing this

Player. CameraMaxZoomDistance = 0

If you want a smooth transition because roblox transitions it smoothly
to go back to normal

Player. CameraMaxZoomDistance = 128