Camera bugs out when it's attached to the player's head

This happens when I toggle the camera to attach to the head it does this

The script is:

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

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

-- Configuration
local CAMERA_ENABLED = true
local TOGGLE_KEY = Enum.KeyCode.C -- Change this to your preferred key
local CAMERA_OFFSET = Vector3.new(0, 0.2, 0.5) -- Offset from head attachment point (slightly up and forward)

-- Variables
local headAttachmentConnection
local isHeadCameraEnabled = false
local originalCameraType

-- Create or get the remote event for toggling
local toggleEvent = ReplicatedStorage:WaitForChild("ToggleHeadCamera", 5)
if not toggleEvent then
	-- If the event doesn't exist, create it (you should create this in ServerScriptService)
	print("Warning: ToggleHeadCamera RemoteEvent not found in ReplicatedStorage")
end

-- Function to attach camera to head
local function attachCameraToHead()
	local character = player.Character
	if not character then return end

	local humanoid = character:FindFirstChild("Humanoid")
	local head = character:FindFirstChild("Head")

	if not humanoid or not head then return end

	-- Store original camera type and settings
	originalCameraType = camera.CameraType

	-- Set camera to scriptable so we can control it
	camera.CameraType = Enum.CameraType.Scriptable

	-- Disable camera collision to prevent spinning
	camera.CameraSubject = nil

	-- Create attachment point on head if it doesn't exist
	local headAttachment = head:FindFirstChild("CameraAttachment")
	if not headAttachment then
		headAttachment = Instance.new("Attachment")
		headAttachment.Name = "CameraAttachment"
		headAttachment.Parent = head
		-- Position it slightly up and forward from the head center
		headAttachment.Position = Vector3.new(0, 0.1, 0.2)
	end

	-- Connect to RenderStepped for smooth camera movement
	headAttachmentConnection = RunService.RenderStepped:Connect(function()
		if head and head.Parent and character.Parent then
			-- Get the world position and rotation of the attachment
			local worldCFrame = headAttachment.WorldCFrame

			-- Apply any additional offset
			local finalCFrame = worldCFrame * CFrame.new(CAMERA_OFFSET)

			-- Set camera CFrame (without collision)
			camera.CFrame = finalCFrame

			-- Set field of view to prevent clipping issues
			camera.FieldOfView = 70
		else
			-- Head was removed, disconnect
			detachCameraFromHead()
		end
	end)

	isHeadCameraEnabled = true
	print("Head camera enabled")
end

-- Function to detach camera from head
local function detachCameraFromHead()
	if headAttachmentConnection then
		headAttachmentConnection:Disconnect()
		headAttachmentConnection = nil
	end

	-- Restore original camera type
	if originalCameraType then
		camera.CameraType = originalCameraType
	else
		camera.CameraType = Enum.CameraType.Custom
	end

	-- Reset camera subject to humanoid for normal camera behavior
	local character = player.Character
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			camera.CameraSubject = humanoid
		end
	end

	-- Reset field of view
	camera.FieldOfView = 70

	isHeadCameraEnabled = false
	print("Head camera disabled")
end

-- Function to toggle head camera
local function toggleHeadCamera()
	if isHeadCameraEnabled then
		detachCameraFromHead()
	else
		attachCameraToHead()
	end
end

-- Handle character spawning
local function onCharacterAdded(character)
	-- Wait for character to fully load
	local humanoid = character:WaitForChild("Humanoid")
	local head = character:WaitForChild("Head")

	-- If head camera was enabled before respawning, re-enable it
	if isHeadCameraEnabled then
		wait(0.1) -- Small delay to ensure everything is loaded
		attachCameraToHead()
	end
end

-- Handle character removal
local function onCharacterRemoving()
	detachCameraFromHead()
end

-- Connect character events
if player.Character then
	onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
player.CharacterRemoving:Connect(onCharacterRemoving)

-- Input handling for manual toggle (optional)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == TOGGLE_KEY then
		toggleHeadCamera()
	end
end)

-- Handle remote event for toggling
if toggleEvent then
	toggleEvent.OnClientEvent:Connect(function(enabled)
		if enabled == nil then
			-- If no parameter passed, toggle
			toggleHeadCamera()
		elseif enabled and not isHeadCameraEnabled then
			-- Enable if not already enabled
			attachCameraToHead()
		elseif not enabled and isHeadCameraEnabled then
			-- Disable if currently enabled
			detachCameraFromHead()
		end
	end)
end

-- Clean up when player leaves
Players.PlayerRemoving:Connect(function(leavingPlayer)
	if leavingPlayer == player then
		detachCameraFromHead()
	end
end)


local function createToggleGui()
	local screenGui = Instance.new("ScreenGui")
	screenGui.Name = "HeadCameraGui"
	screenGui.Parent = player.PlayerGui

	local toggleButton = Instance.new("TextButton")
	toggleButton.Name = "ToggleButton"
	toggleButton.Size = UDim2.new(0, 150, 0, 30)
	toggleButton.Position = UDim2.new(0, 10, 0, 10)
	toggleButton.Text = "Toggle Head Camera (C)"
	toggleButton.Parent = screenGui

	toggleButton.MouseButton1Click:Connect(toggleHeadCamera)

	-- Update button text based on state
	RunService.Heartbeat:Connect(function()
		toggleButton.Text = isHeadCameraEnabled and "Head Camera: ON (C)" or "Head Camera: OFF (C)"
	end)
end

-- Uncomment the line below if you want a GUI toggle button
-- createToggleGui()

-- Export functions for external use
_G.HeadCameraSystem = {
	toggle = toggleHeadCamera,
	enable = function() 
		if not isHeadCameraEnabled then
			attachCameraToHead()
		end
	end,
	disable = function()
		if isHeadCameraEnabled then
			detachCameraFromHead()
		end
	end,
	isEnabled = function()
		return isHeadCameraEnabled
	end
}

im gonna be honest the best solution for this is to… ask chatgpt to be honest. and please, please, make the code readable inside of a code block like this

sorry i mean i would try to help if i could read the code but i can’t

and chatgpt is usually pretty good at this kinda stuff as long as you know what the heck is going on and you can debug as well

Ok I put it in a code block so you can read it now