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)
-- 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
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