Weird First Person Body Glitch

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

  1. What do you want to achieve? Keep it simple and clear!
    I’m making a first person body script, where you can toggle first person and third person.

  2. What is the issue? Include screenshots / videos if possible!
    In third person it works perfectly fine, but when I go into first person, my collisions mess up and I can walk through walls.
    image

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried asking ChatGPT to view my script, but the solutions it tried didn’t work.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil
local runService = game:GetService("RunService")
local input = game:GetService("UserInputService")
local players = game:GetService("Players")

CanToggleMouse = {allowed = true; activationkey = Enum.KeyCode.F;}
CanViewBody = true
Sensitivity = 0.2
Smoothness = 0.05
FieldOfView = 90

local cam = game.Workspace.CurrentCamera
local player = players.LocalPlayer
local m = player:GetMouse()
m.Icon = "http://www.roblox.com/asset/?id=569021388"
local character = player.Character or player.CharacterAdded:Wait()
local humanoidpart = character:WaitForChild("HumanoidRootPart")
local head = character:WaitForChild("Head")
local Face = head.face

local CamPos, TargetCamPos = cam.CoordinateFrame.p, cam.CoordinateFrame.p
local AngleX, TargetAngleX = 0, 0
local AngleY, TargetAngleY = 0, 0

local running = true
local freemouse = false
local thirdPerson = false -- Toggle state for camera mode

Face.Parent = script

function updatechar()
	for _, v in pairs(character:GetChildren()) do
		if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
			if v.Name == "Head" then
				v.LocalTransparencyModifier = thirdPerson and 0 or 1 -- Hide head in first-person
			else
				v.LocalTransparencyModifier = 0 -- Keep the rest of the body visible
			end
			v.CanCollide = thirdPerson -- Enable collisions in third-person
		end

		if v:IsA("Accessory") or v:IsA("Hat") then
			local handle = v:FindFirstChild("Handle")
			if handle then
				handle.LocalTransparencyModifier = thirdPerson and 0 or 1 -- Hide accessories in first-person
				handle.CanCollide = thirdPerson
			end
		end
	end
end



input.InputChanged:Connect(function(inputObject)
	if not thirdPerson and inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		local delta = Vector2.new(inputObject.Delta.x / Sensitivity, inputObject.Delta.y / Sensitivity) * Smoothness
		local X = TargetAngleX - delta.y
		TargetAngleX = math.clamp(X, -80, 80)
		TargetAngleY = (TargetAngleY - delta.x) % 360
	end
end)

input.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.Keyboard then
		-- Check if the player is typing in chat or any other text box
		if input:GetFocusedTextBox() then return end -- Don't toggle if typing

		if inputObject.KeyCode == CanToggleMouse.activationkey then
			freemouse = not freemouse
		elseif inputObject.KeyCode == Enum.KeyCode.V then
			thirdPerson = not thirdPerson
			if thirdPerson then
				cam.CameraType = Enum.CameraType.Custom -- Enable normal third-person camera
				game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default
				character.Humanoid.AutoRotate = true
				Face.Parent = head
				
			else
				cam.CameraType = Enum.CameraType.Scriptable -- Lock to first-person view
				game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter
				character.Humanoid.AutoRotate = false
				Face.Parent = script
			end
			updatechar()
		end
	end
end)



runService.RenderStepped:Connect(function()
	if running then
		updatechar()

		if not thirdPerson then
			CamPos = CamPos + (TargetCamPos - CamPos) * 0.28
			AngleX = AngleX + (TargetAngleX - AngleX) * 0.35
			local dist = TargetAngleY - AngleY
			dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist
			AngleY = (AngleY + dist * 0.35) % 360

			cam.CoordinateFrame = CFrame.new(head.Position)
				* CFrame.Angles(0, math.rad(AngleY), 0)
				* CFrame.Angles(math.rad(AngleX), 0, 0)
				* CFrame.new(0, 0.8, 0)

			humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0, math.rad(AngleY), 0)
			game:GetService("UserInputService").MouseBehavior = freemouse and Enum.MouseBehavior.Default or Enum.MouseBehavior.LockCenter
		end
	end

	cam.FieldOfView = FieldOfView
end)