Problem when resetting camera to default mode

I’m working on a 3rd person shooting system, such that when you equip a gun, the camera changes from the standard mode to a over-the-shoulder type. When you unequip the gun, it should go back to the default mode. The problem is that, when I unequip the gun, it goes back to the default mode but I am not able to rotate the camera using right-click. I tried setting the camera type to custom, the cameramode to default, disconnecting the render step function that controlled the camera movement when using a gun, etc… Here’s the code:

--Made by aStRoJim

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")
local gun = script.Parent
local mouse = player:GetMouse()

local damage = gun.Configuration.Damage.Value
local range = gun.Configuration.Range.Value

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(3, 2, 5)

camera = workspace.CurrentCamera

--[[ ANTI-TRANSPARENT ARMS IN FIRST PERSON ]]--

function antiTrans(part)
	if part and part:IsA("BasePart") and( part.Name=="LeftUpperArm"  or part.Name=="LeftLowerArm" or part.Name=="LeftHand" or part.Name=="RightUpperArm" or part.Name=="RightLowerArm" or part.Name=="RightHand") then -- checks if a part and is a arm
		part.LocalTransparencyModifier = part.Transparency
		part.Changed:connect(function (property)    
			part.LocalTransparencyModifier = part.Transparency--Changes the local modifyer(client side only)
		end)
	end
end

for _,v in pairs(char:GetChildren()) do
	antiTrans(v) -- adds all parts
end

--[[ MAIN ]]--

local activeAnims
holdingGun = player:WaitForChild("HoldingGun").Value
local animTrack

wait(2)
repeat wait() until char:WaitForChild("UpperTorso"):FindFirstChild("Waist", true)
local waist = char:WaitForChild("UpperTorso"):FindFirstChild("Waist")
local rootPart = char:WaitForChild("HumanoidRootPart")
local yOffset = waist.C0.Y

local leftShoulder = char:FindFirstChild("LeftShoulder", true)
local l_yOffset = leftShoulder.C0.Y
local rightShoulder = char:FindFirstChild("RightShoulder", true)
local r_yOffset = rightShoulder.C0.Y

local cameraAngleX = 0
local cameraAngleY = 0

local neck = char:FindFirstChild("Neck", true)
local n_yOffset = neck.C0.Y

go = false

function makeRayVisible(ray)
	local midpoint = ray.Origin + ray.Direction / 2

	local part = Instance.new("Part", workspace)
	part.Anchored = true
	part.CFrame = CFrame.new(midpoint, ray.Origin)
	part.Size = Vector3.new(0.1, 0.1, ray.Direction.Magnitude)
	part.BrickColor = BrickColor.new("Really red")
	part.CanCollide = false

end

gun.Equipped:Connect(function(mouse)
	go = true	
	animTrack = game.Workspace[player.Name].Humanoid:LoadAnimation(gun.Idle)
	animTrack:Play()
	holdingGun = true
	
	local startCamLook = camera.CFrame.LookVector
	local xI,yI,zI = CFrame.new(Vector3.new(0,0,0), startCamLook):ToOrientation()
	local startCamOri = Vector3.new(math.deg(xI), math.deg(yI), math.deg(zI))
	
	cameraAngleX = startCamOri.X
	cameraAngleY = startCamOri.Y
	
	player.PlayerGui.Gun.Frame.Visible = true
	
	humanoid.AutoRotate = false

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	
	if camera.CameraType ~= Enum.CameraType.Scriptable then
		camera.CameraType = Enum.CameraType.Scriptable
	end

	local function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		if inputState == Enum.UserInputState.Change and holdingGun then
			cameraAngleX = cameraAngleX - inputObject.Delta.X
			-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
			cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
			-- Rotate root part CFrame by X delta
			rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
			
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
	
	local function aim(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Begin and holdingGun then
			for i = 0, 1, 0.1 do
				camera.FieldOfView = 70 + (50-70)*i
				neck.C0 = neck.C0:Lerp(CFrame.new(neck.C0.Position) * CFrame.Angles(0, 0, math.rad(-30)), i)
				wait()
			end	
		end
		if inputState == Enum.UserInputState.End and holdingGun then
			for i = 0, 1, 0.1 do
				camera.FieldOfView = 50 + (70-50)*i
				neck.C0 = neck.C0:Lerp(CFrame.new(neck.C0.Position) * CFrame.Angles(0, 0, 0), i)
				wait()
			end	
		end
	end
	ContextActionService:BindAction("Aim", aim, false, Enum.UserInputType.MouseButton2)
	
	local function shoot(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Begin and holdingGun then
			local ray = Ray.new(camera.CFrame.Position, camera.CFrame.LookVector.Unit * range)
			--makeRayVisible(ray)
			local rayParameters = RaycastParams.new()
			rayParameters.IgnoreWater = true
			local rayResult = workspace:Raycast(ray.Origin, ray.Direction, rayParameters)
			local hit
			if rayResult then
				hit = rayResult.Instance
			end
			
			game.ReplicatedStorage.Shoot:FireServer(gun, rayResult.Position, hit, ray.Direction)
		end
	end
	ContextActionService:BindAction("Shoot", shoot, false, Enum.UserInputType.MouseButton1)
	
	--rootPart.CFrame = rootPart.CFrame * CFrame.Angles(math.asin(cameraDirection.y), 0, 0)
	
	wait()
	go = false	
	
	print("j")
end)

local RenderConnection = RunService.RenderStepped:Connect(function()
	
	local cameraDirection = root.CFrame:toObjectSpace(camera.CFrame).lookVector
	
	if waist and holdingGun then
		--print("h")
		local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
		local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -100000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
		
		waist.C0 = CFrame.new(0, yOffset, 0) * CFrame.Angles(math.asin(cameraDirection.y), 0, 0)
		if go then
			rootPart.CFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(math.asin(cameraDirection.y), 0, 0)
		end
	end
end)

gun.Unequipped:Connect(function(mouse)
	RenderConnection:Disconnect()
	
	holdingGun = false
	animTrack:Stop()
	player.CameraMode = Enum.CameraMode.Classic
	camera.CameraType = Enum.CameraType.Custom
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	
2 Likes

It should work, as I’ve understood your script.
However, is it recommended to put functions within functions?
I have got many problems with that, maybe try to rewrite them outside any function.
Then, ask the function you want for to call the appropriate function.

Functions within functions should leave to duplication problems that are hard to solve.
I’m kind of begginer so I really don’t know if it will work. I couldn’t understand all your script as I don’t know everything you’ve used in yet.

1 Like