First person camera crashes roblox studio

I’m making a FPS camera but when I run it, the code either works, doesn’t work or just straight up crashes the roblox studio.

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local character = player.Character

if not character then
	wait()
end

local cameraVariables = require(script.Parent.CameraVariables)

local maxVerticalAngle = math.rad(80)
local maxX = math.rad(80)
local sensitivity = 0.2

local CanViewBody = true

local function FPSCam()
	local head = character:WaitForChild("Head")
	local cameraOffsetVariable = cameraVariables.cameraOffset
	cameraOffsetVariable = Vector3.new(0,1,0)

	local cameraOffset = cameraOffsetVariable

	camera.CameraType = Enum.CameraType.Custom

	for _, v in pairs(character:GetChildren())do
		if CanViewBody then
			if v.Name == 'Head' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = false
				if v:FindFirstChild("face") then
					v.face.LocalTransparencyModifier = 1
				elseif v:FindFirstChild("SurfaceAppearance") and v:FindFirstChild("FaceControls") then
					v.SurfaceAppearance:Destroy()
					v.FaceControls:Destroy()
				end
			end
		else
			if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then
				v.LocalTransparencyModifier = 1
				v.CanCollide = false
			end
		end
		if v:IsA'Accessory' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end
		if v:IsA'Hat' then
			v:FindFirstChild('Handle').LocalTransparencyModifier = 1
			v:FindFirstChild('Handle').CanCollide = false
		end
	end

	RunService:BindToRenderStep("FPSCamera", Enum.RenderPriority.Camera.Value, function()
		maxX -= UIS:GetMouseDelta().X * sensitivity
		maxVerticalAngle = math.clamp(maxVerticalAngle - UIS:GetMouseDelta().Y * sensitivity, -75, 75)

		local startCFrame = CFrame.new(head.CFrame.Position) * CFrame.Angles(0, math.rad(maxX), 0) * CFrame.Angles(math.rad(maxVerticalAngle), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -100000))

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

	CAS:BindAction("FPSCam", FPSCam, false, Enum.KeyCode.C)
end

it also crashes when i try to unbind the runservice

2 Likes
CAS:BindAction("FPSCam", FPSCam, false, Enum.KeyCode.C)

Is inside the FPSCam function so it cannot actually be called

2 Likes

Thanks, I don’t know how I didn’t see that

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.