Camera Mode Player Issues

Hello, I am about to finish a practice project to learn and I need a hand urgent!

I need to condition this localscript to only work in mode:
player.CameraMode = Enum.CameraMode.LockFirstPerson

Any idea how to do that?

I want it to only work if that mode is activated, if it’s classic mode then it won’t work…

localscript:

-- services
local runService = game:GetService("RunService")

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

-- character stuff
local humanoid = character:WaitForChild("Humanoid")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local torso = character:WaitForChild("Torso")

-- put all motors inside the character into a dictionary for easy access
-- remove spaces from motor names
local motors = {}
for _, motor in pairs(character:GetDescendants()) do
	if motor:IsA("Motor6D") then
		-- gives a table with the motor as the first index, and the motor initial c0 as the second
		motors[string.gsub(motor.Name, "%s+", "")] = {motor, motor.C0}
	end
end

-- update function
function renderStepped(dt)
	-- get useful motor6ds
	local root = motors["RootJoint"]
	local leftHip = motors["LeftHip"]
	local rightHip = motors["RightHip"]
	-- get angles of camera
	local y, x, z = camera.CFrame:ToEulerAnglesYXZ()
	local groundOffset = math.abs(0.5 * torso.Size.Y - 0.5 * torso.Size.Z) * math.abs(math.sin(y))
	-- rotate motor6ds
	root[1].C0 = root[2] * CFrame.fromEulerAnglesYXZ(-y, 0, 0) * CFrame.new(0, 0, -groundOffset)
	leftHip[1].C0 = leftHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, y)
	rightHip[1].C0 = rightHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, -y)
end

-- connections
local connection = runService:BindToRenderStep("updateMotors", Enum.RenderPriority.Character.Value, renderStepped)

-- events
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local lookDownEvent = ReplicatedStorage:WaitForChild("lookDownEvent")

lookDownEvent:FireServer(player, connection)

I am confused, so you want the localscript to work if the player’s camera is locked to first person?

Yes, I would like it to only work when the player’s camera is this:
player.CameraMode = Enum.CameraMode.LockFirstPerson

and in the opposite case (if it is classic) I would like this not to work

if player.CameraMode == Enum.CameraMode.LockFirstPerson then
--Code In This Condition--
end

So this doesn’t work/Have you tried this?

-- update function
function renderStepped(dt)
	if player.CameraMode == Enum.CameraMode.LockFirstPerson then
		print("Correct camera mode.")
		-- get useful motor6ds
		local root = motors["RootJoint"]
		local leftHip = motors["LeftHip"]
		local rightHip = motors["RightHip"]
		-- get angles of camera
		local y, x, z = camera.CFrame:ToEulerAnglesYXZ()
		local groundOffset = math.abs(0.5 * torso.Size.Y - 0.5 * torso.Size.Z) * math.abs(math.sin(y))
		-- rotate motor6ds
		root[1].C0 = root[2] * CFrame.fromEulerAnglesYXZ(-y, 0, 0) * CFrame.new(0, 0, -groundOffset)
		leftHip[1].C0 = leftHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, y)
		rightHip[1].C0 = rightHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, -y)
	else
		print("Incorrect camera mode.")
		--do whatever
	end
end

Added the conditional check to the function.

2 Likes

It works but I have a bug, when I change the camera, is there a way in the else to make the pos reset to normal?

image

The position of what? The camera or player?

When I am in the first person and I look up, then I change to the third person, this happens that the player is in the position of how I left it in the first person

This is likely because your script is not resetting the character’s position when they leave first person mode.

I don’t really understand how I could do to reset to the normal position? :frowning:

Something like this?

-- update function
function renderStepped(dt)
	if player.CameraMode == Enum.CameraMode.LockFirstPerson then
		print("Correct camera mode.")
		-- get useful motor6ds
		local root = motors["RootJoint"]
		local leftHip = motors["LeftHip"]
		local rightHip = motors["RightHip"]
		-- get angles of camera
		local y, x, z = camera.CFrame:ToEulerAnglesYXZ()
		local groundOffset = math.abs(0.5 * torso.Size.Y - 0.5 * torso.Size.Z) * math.abs(math.sin(y))
		-- rotate motor6ds
		root[1].C0 = root[2] * CFrame.fromEulerAnglesYXZ(-y, 0, 0) * CFrame.new(0, 0, -groundOffset)
		leftHip[1].C0 = leftHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, y)
		rightHip[1].C0 = rightHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, -y)
	else
		print("Incorrect camera mode.")
		--do whatever
		local root = motors["RootJoint"]
		local leftHip = motors["LeftHip"]
		local rightHip = motors["RightHip"]
		-- get angles of camera
		-- rotate motor6ds
		root[1].C0 = root[2] * CFrame.fromEulerAnglesYXZ(0, 0, 0) * CFrame.new(0, 0, 0)
		leftHip[1].C0 = leftHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, 0)
		rightHip[1].C0 = rightHip[2] * CFrame.fromEulerAnglesYXZ(0, 0, 0)
	end
end