Mouse upwards/downwards rotation being disabled after right clicking

Hello everyone! It’s my first post after reading tons and tons and not finding a solution to my problem. I’ve made this cool OTS (Over-the-shoulder) camera system which works perfectly fine, aside when I implemented an aiming function. Basically what I’m doing for aiming is just setting the camera’s CFrame to a part’s CFrame.

Setting the CFrame of the camera works fine, the only problem I have is that I literally can’t move my mouse up or down after aiming. I’ll send a video of the problem aswell.

This is the code that I’ve used so far:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Cam = game.Workspace.CurrentCamera
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local x = 0
local y = 0
local offset = Vector3.new(3, 3, 8)
local aiming = false

local startCFrame
local cameraCFrame
local cameraDirection
local render
local inputs

local function camera(active)
	if active == true then
		Mouse.Icon = "rbxassetid://4625402759"
		Humanoid.AutoRotate = false
		Cam.CameraType = Enum.CameraType.Scriptable
		inputs = UIS.InputChanged:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseMovement then
				x = x - input.Delta.X
				y = math.clamp(y - input.Delta.Y*0.3, -75, 75)
				HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(-input.Delta.X), 0)
			end
		end)
		UIS.InputBegan:Connect(function(input)
			if input.UserInputType == Enum.UserInputType.MouseButton2 then
				if not aiming then
					aiming = true
					wait(.2)
				elseif aiming then
					aiming = false
					wait(.2)
				end
			end
		end)
		render = RunService.RenderStepped:Connect(function()
			if not aiming then
				UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

				startCFrame = CFrame.new((HumanoidRootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(x), 0) * CFrame.Angles(math.rad(y), 0, 0)
				cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(offset.X, offset.Y, offset.Z))
				cameraDirection = startCFrame:ToWorldSpace(CFrame.new(3, 3, -100))

				Cam.CFrame = CFrame.new(cameraCFrame.Position, cameraDirection.Position)
			elseif aiming then
				Cam.CFrame = arma.Mira.CFrame
			end
		end)
		wait(.1)
		HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.Position, Vector3.new(cameraDirection.X, 1, cameraDirection.Z))
	else
		wait(.1)
		Humanoid.AutoRotate = true
		UIS.MouseBehavior = Enum.MouseBehavior.Default
		Cam.CameraType = Enum.CameraType.Custom
		Mouse.Icon = ""
		render:Disconnect()
		inputs:Disconnect()
	end
end

And here’s the visualization of the problem:


If anyone could help me point out what’s wrong with my script, or where I am restraining mouse movement upwards/downwards I would be really grateful. Thanks! :slight_smile:

This does not get disabled when you stop aiming.

What do you mean? I need it to be locked to the center of the screen at all times, while aiming and while not aiming. What should I do?

Adding a print statement to detect the delta of the Y movement of the mouse I found out that it’s getting set back to the default position (arma.Mira.CFrame), basically ignoring the mosue input and setting it back to the CFrame.