How to make camera lock onto player

I’m trying to get my Over the shoulder camera to lock onto player but I can’t seem to figure it out, Here is what I got so far

local StartCFrame = CFrame.new((HumanoidRootPart.CFrame.p + Vector3.new(0, 2, 0))) * CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0)
local Offset = Camera.CameraSettings.Offset
local CameraCFrame = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, Offset.Z))
local CameraFocus = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, -50000))
if (CombatProperties.Target.Value ~= nil) then
				
	local TargetRoot = CombatProperties.Target.Value.PrimaryPart
	CameraCFrame = CFrame.new(HumanoidRootPart.Position, TargetRoot.Position)

end
local NewCameraCFrame = CFrame.new(CameraCFrame.p, CameraFocus.p) * (ShakeCFrame.Value)

When the Target objectvalue has a player inside it the camera doesn’t seem to lock on ?

2 Likes
local StartCFrame = CFrame.new((HumanoidRootPart.CFrame.p + Vector3.new(0, 2, 0))) * CFrame.Angles(0, math.rad(xAngle), 0) * CFrame.Angles(math.rad(yAngle), 0, 0)
local Offset = Camera.CameraSettings.Offset
local CameraCFrame = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, Offset.Z))
local CameraFocus = StartCFrame + StartCFrame:VectorToWorldSpace(Vector3.new(Offset.X, Offset.Y, -50000))

if CombatProperties.Target.Value then
    local TargetRoot = CombatProperties.Target.Value:FindFirstChild("HumanoidRootPart")
    if TargetRoot then
        CameraCFrame = CFrame.new(HumanoidRootPart.Position, TargetRoot.Position)
    end
end

local NewCameraCFrame = CFrame.new(CameraCFrame.p, CameraFocus.p) * (ShakeCFrame.Value)

Changes made:

  1. Checked if CombatProperties.Target.Value is not nil and if it has a HumanoidRootPart.
  2. Used :FindFirstChild("HumanoidRootPart") to get the HumanoidRootPart of the target player.

Ensure that the CombatProperties.Target.Value is set correctly to the player you want to target. If you continue to experience issues, you may need to debug further by printing values or checking for errors in the output. Correct me if im wrong or if i didnt fully understand what u meant