I am having trouble trying to attach the camera to the player’s head. What am I doing wrong, and how can I fix it?
module script:
-- CameraSwitchModule
local CameraSwitchModule = {}
-- Constants
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()
-- Get the player's character and camera
local character = player.Character
local camera = workspace.CurrentCamera
-- Check if the character and camera exist
if character and camera then
-- Get the character's head
local head = character:FindFirstChild("Head")
-- Check if the head exists
if head then
-- Set the camera as a descendant of the head
camera.CFrame = head.CFrame * CFrame.new(0, 0, -5) -- Adjust camera offset as needed
end
end
end
local function updateThirdPersonCamera()
-- Get the player's character and camera
local character = player.Character
local camera = workspace.CurrentCamera
-- Check if the character and camera exist
if character and camera then
-- Set the camera as a descendant of the character's humanoid root part
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
camera.CFrame = humanoidRootPart.CFrame * CFrame.new(0, 5, -10) -- Adjust camera offset as needed
end
end
end
function CameraSwitchModule.SetFirstPersonWithMouse()
-- Set the camera to first person
game.Workspace.CurrentCamera.CameraType = FIRST_PERSON
currentCameraType = FIRST_PERSON
-- Set the camera as a descendant of the head
updateFirstPersonCamera()
end
function CameraSwitchModule.SetThirdPerson()
-- Set the camera to third person
game.Workspace.CurrentCamera.CameraType = THIRD_PERSON
currentCameraType = THIRD_PERSON
-- Set the camera as a descendant of the humanoid root part
updateThirdPersonCamera()
end
function CameraSwitchModule.ToggleCamera()
-- Toggle between first and third person
if currentCameraType == FIRST_PERSON then
CameraSwitchModule.SetThirdPerson()
else
CameraSwitchModule.SetFirstPersonWithMouse()
end
end
return CameraSwitchModule
video issues: