Is it possible to change how far zoomed in the camera is with a script?

Greetings,

I am making a sniper rifle and am in the process of creating the scope function. When the player holds RMB the FOV is set to 10 and the camera mode is LockFirstPerson. When they let go the FOV and camera mode return to what they were before, however, i can’t seem to change how far the camera is zoomed in. Thus right now the player has to manually scroll with the mouse to zoom out the camera which is what I am trying to prevent.

local FoV = camera.FieldOfView
mouse.Button2Down:Connect(function()
player.CameraMode = “LockFirstPerson”
–camera.CFrame = tool.Hole.CFrame
camera.FieldOfView = 10
player.PlayerGui.SC.Frame.Visible = true
end)
mouse.Button2Up:Connect(function()

  player.CameraMode = "Classic"
  
  --camera.CameraType = Enum.CameraType.Custom
  camera.FieldOfView = FoV
  local Root = player.Character.HumanoidRootPart
  --local playerPosition = player.Character.HumanoidRootPart.Position
  --local cameraPosition = playerPosition + OFFSET
  
  --camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
  player.PlayerGui.SC.Frame.Visible = false
  --camera.CFrame = player.Character.UpperTorso.CFrame * CFrame.new(0,0,-5)

end)

I’ve tried manipulating the camera focus and cframe and even humanoid.CameraOffset but none of these work, thus I am asking if somebody knows how I would set the camera position to what it was before the player used the scope

Thanks for your time

The best way to check (personally) is to check the magnitude to the head, because the camera usually zooms into the head for first person

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local distance = 1
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")

if math.abs((head.CFrame.p - camera.CFrame.p).Magnitude) <= distance then
    -- Your code
end

Edit: I forgot you were talking about a sniper LOL

I am sorry I don’t quite understand what you’re getting at. I don’t want to check anything i just want to set the camera zoom distance without using the mouse wheel

set the player’s CameraMinZoomDistance and CameraMaxZoomDistance of the player, along with the code posted in reply #1:

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local distance = 1
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")

local OldDist = (head.CFrame.p - camera.CFrame.p).Magnitude
-- the code you have to zoom in

--whatever zooms you out
player.CameraMaxZoomDistance = OldDist
player.CameraMinZoomDistance = OldDist
camera.FieldOfView = DefaultFov

That’s such a simple solution, thank you! i just set the max and min zoom to the distance i want then put a wait() and set them to what they are supposed to be for all players