Lua First-Person Camera Issues

Hello everyone! I need a hand with my Lua code.

I would like to create a first-person camera, but I am experiencing some issues, and it is not working correctly. It positions itself on my player’s head but does not move along with the first-person camera.

Additionally, I have errors in displaying the mouse icon in third person. What could be wrong? I’ve attached images of the errors as well.

-- CameraSwitchModule

local CameraSwitchModule = {}

local FIRST_PERSON = Enum.CameraType.Scriptable
local THIRD_PERSON = Enum.CameraType.Custom

-- Variables
local player = game.Players.LocalPlayer
local currentCameraType = FIRST_PERSON

-- Functions

local function updateFirstPersonCamera()
	local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local lastMousePosition = Vector2.new(player:GetMouse().X, player:GetMouse().Y)
		local sensitivity = 0.1 -- Adjust sensitivity
		local verticalLimit = math.rad(60) -- Vertical movement limit

		local function updateCamera()
			local currentMousePosition = Vector2.new(player:GetMouse().X, player:GetMouse().Y)
			local mouseDelta = (currentMousePosition - lastMousePosition) * sensitivity

			-- Update head orientation based on mouse movement
			local newHeadCFrame = CFrame.new(humanoid.Parent.Head.Position)
				* CFrame.Angles(math.clamp(math.rad(-mouseDelta.y), -verticalLimit, verticalLimit), 0, 0)
				* CFrame.Angles(0, math.rad(-mouseDelta.x), 0)

			humanoid.Parent.Head.CFrame = newHeadCFrame

			-- Update camera position to be at the head
			game.Workspace.CurrentCamera.CFrame = CFrame.new(humanoid.Parent.Head.Position)

			-- Hide the cursor
			game.Players.LocalPlayer.PlayerGui.MouseIcon = ""
		end

		local runService = game:GetService("RunService")
		local connection = runService.RenderStepped:Connect(updateCamera)

		player.CharacterAdded:Connect(function(char)
			humanoid = char:WaitForChild("Humanoid")
		end)

		player.CharacterRemoving:Connect(function()
			connection:Disconnect()
		end)
	end
end

function CameraSwitchModule.SetFirstPersonWithMouse()
	-- Set the camera to first person
	game.Workspace.CurrentCamera.CameraType = FIRST_PERSON
	currentCameraType = FIRST_PERSON

	-- Adjust camera properties for first-person perspective
	updateFirstPersonCamera()
end

function CameraSwitchModule.SetThirdPerson()
	-- Set the camera to third person
	game.Workspace.CurrentCamera.CameraType = THIRD_PERSON
	currentCameraType = THIRD_PERSON

	-- Show the default cursor when switching to third person
	game.Players.LocalPlayer.PlayerGui.MouseIcon = "rbxasset://SystemCursors/Arrow"
end

function CameraSwitchModule.ToggleCamera()
	-- Toggle between first and third person
	if currentCameraType == FIRST_PERSON then
		CameraSwitchModule.SetThirdPerson()
	else
		CameraSwitchModule.SetFirstPersonWithMouse()
	end
end

return CameraSwitchModule
1 Like

What you are doing:

	game.Players.LocalPlayer.PlayerGui.MouseIcon = "rbxasset://SystemCursors/Arrow"

What should be done:

local player = game.Playrs.LocalPlayer
local mouse = player:GetMouse()
mouse.Icon = "rbxasset://SystemCursors/Arrow"
2 Likes

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