Need help with my simple zoom into ironsight script

Hello, Im trying to make a script when the player is zoomed in and clicks the right mouse button, the player will look through the scope. I have tried setting the players camera CFrame to the ironsight part but when I click the right mouse button, the camera keeps spinning. I’ve used quenty’s perfection weld script to weld all parts together. Piece of the code:

local player = game:GetService("Players").LocalPlayer
local Camera = workspace.CurrentCamera
local gunScope = script.Parent:WaitForChild("sightaim")

Mouse.Button2Down:connect(function()
--ignore the code below, its just checking if the player is zoomed in.
if not Reloading and not HoldDown and AimDown == false and Equipped == true and  Module.IronsightEnabled and (Camera.Focus.p-Camera.CoordinateFrame.p).magnitude <= 1 then 
			
Camera.CFrame= gunScope.CFrame
end)

The script looks all right, but is the scope facing the front? (Check within the properties). Also, the camera replicates one CFrame of the scope’s: What I mean is that the camera isn’t constantly getting the Scope’s CFrame, it only replicates it once. Try a loop instead (repeat until); I see also you didn’t set the Camera’s type to a scriptable.
Camera.CameraType
TLDR: Conclusion: Loop the CFrame and change the Camera’s type.

In this case, I think changing the Camera’s FOV would be more appropriate.

Changing the cframe would make it seem like your character has moved, which isn’t the case.

1 Like

To elaborate on what jrelvas said, you could do something like

local player = game:GetService("Players").LocalPlayer
local Camera = workspace.CurrentCamera
local Mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local defaultFOV = Camera.FieldOfView
local targetFOV = 40
local MouseIsDown = false

Mouse.Button2Down:Connect(function()
	MouseIsDown = true
    repeat wait()
      Camera.FieldOfView = Camera.FieldOfView - 1
    until (Camera.FieldOfView<=targetFOV) or not MouseIsDown
end)

Mouse.Button2Up:Connect(function()
	 MouseIsDown = false
	Camera.FieldOfView = defaultFOV
end)
1 Like

Hey, thanks for your reply. I think I’ll stick with changing the FOV, however how do I make like it looks through the Scope union? Thanks.

You could tween down the Player’s CameraMaxzoomDistance.

oldMaxZoom = player.CameraMaxZoomDistance
for i=0,10 do wait()
  player.CameraMaxZoomDistance = oldMaxZoom*(10-i/10)
end

Something like this

I feel like zooming in like this could be done easier with TweenService

But to contribute: Use TweenService instead of incrementing with for loops. (Unless you have to for countdowns.)

I tend to use tweenservice for more complicated things and loops for simple linear things like this.

I would especially consider using loops for a gun zoom animation because the user might want to exit the zoom in the middle of the loop. I guess you could always use Tween:Stop()

1 Like

You dont even need to use Tween:Stop() it automatically overwrites the current tween if you retween it. but ye like I said for loops and tweenservice are for specific things.

Thanks all for your reply. Ill try it out.