Weird camera glitch when using code from the wiki

Hey, so I’ve been trying to make a working third person camera for a gun for the past day or so. I’ve recently found some working code from the roblox developer hub here, I used the code from the “Over-the-shoulder” section and tweaked it a little bit to correspond with my gun. Though I get a weird outcome when the player unequips the gun shown here:

I find this to be very weird as I’ve unbinded everything that uses ContextActionService when the player unequips the gun and I also terminated the RunService. I don’t know why this is happening, if somebody could help me that would be great.

Code:

local Players = game:GetService("Players")
ContextActionService = game:GetService("ContextActionService")
UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
	 
camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(2, 4, 8)
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.CharacterAdded:Wait()
 
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
humanoid.AutoRotate = false
 
local cameraAngleX = 0
local cameraAngleY = 0

script.Parent.Equipped:Connect(function()
	script.Parent.GetAmmo:FireServer()
 
	function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		if inputState == Enum.UserInputState.Change 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)
 
	Stepped = RunService.RenderStepped:Connect(function()
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		camera.CameraType = Enum.CameraType.Scriptable
		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, -10000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
	end)
end)
 
local function focusControl(actionName, inputState, inputObject)
	-- Lock and hide mouse icon on input began
	if inputState == Enum.UserInputState.Begin then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = false
		ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
	end
end

script.Parent.Unequipped:Connect(function()
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	player.CameraMode = Enum.CameraMode.Classic
	camera.CameraType = Enum.CameraType.Custom
	Stepped:Disconnect()
	ContextActionService:UnbindAction(focusControl)
	ContextActionService:UnbindAction(playerInput)
end)

Try changing the camerasubject back to the players humanoid.

Didn’t do anything, no errors.

This is very weird, hold on let me try something…

I’ve actually fixed the part where when you turn the mouse the character moves with it, but the part where the character doesn’t point towards the direction of the key you pressed still isn’t fixed. Like for example if I pressed A my character would turn left and go left or if I pressed D it would turn right and go right but my character doesn’t turn in just sidewalks.

The camera is very messed up, things such as when the player dies the camera doesn’t reset. Also when your root moves for some reason such as crashing into walls the camera doesn’t go with it. Here is an edited code to prevent those. I’m not sure about your current problem but I will help you terniminate the problems I faced.

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, 4)
local player = Players.LocalPlayer
 
local humanoid, character, rootPart, cameraAngleX, cameraAngleY

local Loaded = false

player.CharacterAdded:Connect(function(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
	rootPart = character:WaitForChild("HumanoidRootPart")
	humanoid.AutoRotate = false
	Loaded = true
end)

repeat 
	wait() 
until Loaded == true
 
 
	local function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		if inputState == Enum.UserInputState.Change then
			-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
		
			--cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -20, 20)
		
			-- 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)
 
	RunService.RenderStepped:Connect(function()
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			camera.CameraType = Enum.CameraType.Scriptable
		end
		local startCFrame = rootPart.CFrame
		local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
		local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)
	end)

 
local function focusControl(actionName, inputState, inputObject)
	-- Lock and hide mouse icon on input began
	if inputState == Enum.UserInputState.Begin then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = false
		ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
	end
end
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

Can you make it so that you can also move on the Y axis instead of just the X axis?

Yah, just use the y axis code from the default example, and add the neccary parts in the script I posted, it should work fine.

What do you mean by the “default example”?

I’m not sure how to allow it to move on the y axis as well. I really suck at camera manipulation, if it isn’t to much of your time could you write a line of code to show me?