Slight camera desync in sight change

Hello, I am trying to create a program that positions the camera to a “Gunner sight” when the LShift key is pressed, and should move with the part, my code is able to do so with some issues however.

My main issue is that whenever the canon comes to a complete halt, the CFrame does not update with it, (I decided to update the CFrame instead of using a cameratype since Attached was not inclusive to Y). I also believe there is a more optimized work around but I can’t think of one right now.

I have tried updating the CFrame each time the loop runs and also looked into alternative ways of moving the camera, yet neither of the results have worked.

This is the program:

local camDB = true

game.UserInputService.InputBegan:Connect(function(input)
		if isGunner then
				
			if input.UserInputType == Enum.UserInputType.MouseButton1 then
				game.ReplicatedStorage.AT_System.FireAT:FireServer() --Fire canon
			end
				
			if input.KeyCode == Enum.KeyCode.LeftShift then --Key for gunner sight
				if camDB then
					local sightCFrame = game.Workspace["AT Gun"].AT_Model.AT_SIGHT.CFrame
					
					camDB = false
					CurrentCamera.CameraType = Enum.CameraType.Scriptable
						
					thisPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
					CurrentCamera.CameraSubject = game.Workspace["AT Gun"].AT_Model.AT_SIGHT
					CurrentCamera.CFrame = CFrame.new(sightCFrame.X, sightCFrame.Y, sightCFrame.Z)
					
				else
					camDB = true
					CurrentCamera.CameraType = Enum.CameraType.Scriptable
					thisPlayer.CameraMode = Enum.CameraMode.Classic
					CurrentCamera.CameraSubject = thisPlayer.Character.Humanoid
					CurrentCamera.CameraType = Enum.CameraType.Custom
				end
			end
				
			while game.UserInputService:IsKeyDown(Enum.KeyCode.D) do
				game.ReplicatedStorage.AT_System.MoveAT:FireServer("D") --Traverse Right
				task.wait(0.001)
			end
				
			while game.UserInputService:IsKeyDown(Enum.KeyCode.A) do
				game.ReplicatedStorage.AT_System.MoveAT:FireServer("A") --Traverse Left
				task.wait(0.001)
			end
				
			while game.UserInputService:IsKeyDown(Enum.KeyCode.W) do
				game.ReplicatedStorage.AT_System.MoveAT:FireServer("W") --Elevate Gun
				task.wait(0.001)
			end
				
			while game.UserInputService:IsKeyDown(Enum.KeyCode.S) do
				if not camDB then
					CurrentCamera.CFrame = game.Workspace["AT Gun"].AT_Model.AT_SIGHT.CFrame 
				end
				game.ReplicatedStorage.AT_System.MoveAT:FireServer("S") --Depress Gun
				task.wait(0.001)
			end
	end
end)

This is the result I get from my program, keep in mind a tween runs to smoothly rotate the canon:

1 Like