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.
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)
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()
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.