OTS Camera pointing issue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? For my camera to point where ever the mouse is

  2. What is the issue? Whenever I right Click whilst my gun is equipped, my camera is mean to update to an Over The Shoulder type of camera, just like GTA. And it does just that, however whenever I actually do right click, my camera points to a very specific part of my map for no reason, I want my camera to look to the area that my mouse is pointing at. Instead, my camera and mouse just update and point to this random spot on the map, and it’s only this one spot. After this happens I am able to of course move it to any desired spot.

  3. What solutions have you tried so far? I’ve looked around for similar problems to mine but have found none. I’m relatively new to scripting and while I do understand my code very well, I don’t understand how I can go about doing what I want to as stated above.

This is my script.


local Players = game:GetService("Players")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local char = player.Character

local tool = script.Parent.Parent

local handle = tool:WaitForChild("Handle")
local FireRemote = tool:WaitForChild("FireEvent")

local AimGunAnim = handle:WaitForChild("AimGun")
local EquipGunAnim = handle:WaitForChild("EquipGun")
local EquipGunIdle = handle:WaitForChild("EquipGunIdle")
local RecoilAnim = handle:WaitForChild("Recoil")

local ContextActionService = game:GetService('ContextActionService')
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

local FireRate = 0.4
local Max_Ammo = 12

local plrgui = player.PlayerGui
local GunMenuScreenGUI = plrgui:WaitForChild("GunMenu")
local AmmoInGunLabel = GunMenuScreenGUI.AmmoInGun
local AmmunitionStoredLabel = GunMenuScreenGUI.AmmunitionStored

local HasShot = false
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer

local cameraOffset = Vector3.new(1.5, 2, 2)
local RightCameraOffset = Vector3.new(1.5, 2, 2)
local LeftCameraOffset = Vector3.new(-1.5, 2, 2)
local HasChangedShoulders = false
local IsAiming = false
local IsEquipped

local char = workspace:WaitForChild(player.Name)
local hum = char:WaitForChild("Humanoid")

--Tween Propterties

local DefaultFOV = 70
local RecoiledFOV = 100

local reloading = false
local ammoLeftVal = tool:WaitForChild("AmmoLeft")
local AmmoStoredVal = tool:WaitForChild("AmmunitionStored")
AmmunitionStoredLabel.Text = AmmoStoredVal.Value

local PropertyAdd = {FieldOfView = DefaultFOV + 30}
local PropertyRevert = {FieldOfView = DefaultFOV}
local TI = TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local TweenAdd = TS:Create(camera, TI, PropertyAdd)
local TweenRevert = TS:Create(camera, TI, PropertyRevert)

-- Animations
local GunIdle = tool.Animations:WaitForChild("IdleEquippedAnim")
local GunIdleTrack = hum:LoadAnimation(GunIdle) 
GunIdleTrack.Priority = Enum.AnimationPriority.Action
GunIdleTrack.Looped = true

local AimGun = tool.Animations:WaitForChild("AimGunAnim")
local AimGunTrack = hum:LoadAnimation(AimGun)
AimGunTrack.Priority = Enum.AnimationPriority.Action
AimGunTrack.Looped = true

local RecoilAnim = tool.Animations:WaitForChild("RecoilAnim")
local RecoilAnimTrack = hum:LoadAnimation(RecoilAnim)
RecoilAnimTrack.Priority = Enum.AnimationPriority.Action
RecoilAnimTrack.Looped = false

local cameraUpdateConnection


AmmoStoredVal.Changed:Connect(function()
	AmmunitionStoredLabel.Text = "Ammo Left: " .. AmmoStoredVal.Value
end)

ammoLeftVal.Changed:Connect(function()
	AmmoInGunLabel.Text = "Ammo Left: " .. ammoLeftVal.Value
end)

local function reload()
	if reloading then return end
	reloading = true

	if AmmoStoredVal.Value > 0 then
		local ammoToAdd = math.min(AmmoStoredVal.Value, Max_Ammo - ammoLeftVal.Value)
		ammoLeftVal.Value += ammoToAdd
		AmmoStoredVal.Value -= ammoToAdd
		reloading = false
	elseif AmmoStoredVal.Value == 0 then
		print("Player does not have enough ammo to reload his weapon")
	end
end



tool.Equipped:Connect(function()
	GunIdleTrack:Play()

	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			GunIdleTrack:Stop()
			AimGunTrack:Play()
			local character = tool.Parent
			local humanoid = character:WaitForChild("Humanoid")
			local rootPart = character:WaitForChild("HumanoidRootPart")

			local cameraAngleX = 0
			local cameraAngleY = 0

			humanoid.AutoRotate = false

			local function playerInput(actionName, inputState, inputObject)
				if inputState == Enum.UserInputState.Change then
					cameraAngleX -= inputObject.Delta.X
					cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
				end
			end

			ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

			cameraUpdateConnection = RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
				local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
				local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
				local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -1000000))

				camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)

				local lookingCFrame = CFrame.lookAt(rootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -10000000000)))

				rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)

				local function focusControl(actionName, inputState, inputObject)
					camera.CameraType = Enum.CameraType.Scriptable
					UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
					UserInputService.MouseIcon = "http://www.roblox.com/asset/?id=10181263678"
				end

				ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton2)

				UserInputService.InputEnded:Connect(function(input)
					if input.UserInputType == Enum.UserInputType.MouseButton2 then
						GunIdleTrack:Play()
						AimGunTrack:Stop()
						
						ContextActionService:UnbindAction("PlayerInput")
						RunService:UnbindFromRenderStep("CameraUpdate")
						ContextActionService:UnbindAction("FocusControl")
						ContextActionService:UnbindAction("Reload")
						camera.CameraType = Enum.CameraType.Custom

						humanoid.AutoRotate = true

						UserInputService.MouseBehavior = Enum.MouseBehavior.Default
						UserInputService.MouseIcon = ""
					end
				end)
			end)


		end

		GunMenuScreenGUI.Enabled = true

		tool.Activated:Connect(function()
			local mousePosition = mouse.Hit.Position

			local function ClientFire()
				local hum = char:WaitForChild("Humanoid")
				if not HasShot and ammoLeftVal.Value > 0 then
					HasShot = true
					FireRemote:FireServer(mousePosition)
					ammoLeftVal.Value -= 1
					RecoilAnimTrack:Play()
					TweenAdd:Play()

					TweenAdd.Completed:Connect(function()
						TweenRevert:Play()
					end)

					task.wait(FireRate)

					HasShot = false
				elseif ammoLeftVal.Value == 0 then
					print("Player has no ammo left")
				elseif HasShot then
					print("Player is currently still shooting.")
				end
			end

			ClientFire()
		end)

		ContextActionService:BindAction("Reload", function(name, state, obj)
			if state == Enum.UserInputState.Begin then
				reload()
			end
		end, false, Enum.KeyCode.R)
	end)

	tool.Unequipped:Connect(function()
		if AimGunTrack and RecoilAnimTrack then
			AimGunTrack:Stop()
			RecoilAnimTrack:Stop()
			GunIdleTrack:Stop()
		end

		GunMenuScreenGUI.Enabled = false
		ContextActionService:UnbindAction("PlayerInput")
		if cameraUpdateConnection then
			RunService:UnbindFromRenderStep(cameraUpdateConnection)
			cameraUpdateConnection = nil
		end
		ContextActionService:UnbindAction("FocusControl")
		ContextActionService:UnbindAction("Reload")
		camera.CameraType = Enum.CameraType.Custom

		hum.AutoRotate = true

		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
		UserInputService.MouseIcon = ""
	end)
end)


Sorry if it feels a little unreadable, I’m going to attempt to clean it up after all of these issues are fixed.
Here is a video of the problem.https://streamable.com/b48551