How to make an camera-target-lock system for a fight game?

I Need to script a camera-lock-system for my soulslike roblox game, if you dont know what is a camera lock system i’ll show you.
I Already have the “click to lock” script, i just need the CFrame stuff.
Example of what i want:

To make a lock on system i’m using this script:

local UIS = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local contextActionService = game:GetService("ContextActionService")

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local HRP = character:WaitForChild("HumanoidRootPart")

local cam = workspace.CurrentCamera
local lockOn = false

local target

function FindNearestHRP(HRP)
local targetDistance = 100
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("Model") and v:FindFirstChild("Humanoid") and v.Name ~= character.Name then
local targetHRP = v.HumanoidRootPart
local Mag = (HRP.Position- targetHRP.Position).Magnitude
if Mag <= targetDistance then
targetDistance = Mag
target = targetHRP
end
end
end
return target
end

local function M2(Action)
if Action == "Off" then
contextActionService:BindActionAtPriority("DisableM2",function()
return Enum.ContextActionResult.Sink
end ,false, Enum.ContextActionPriority.High.Value,Enum.UserInputType.MouseButton2)
elseif Action == "On" then
contextActionService:BindActionAtPriority("DisableM2",function()
return Enum.ContextActionResult.Pass
end ,false, Enum.ContextActionPriority.High.Value,Enum.UserInputType.MouseButton2)
end
end
function OnMobilePress()
if lockOn then
lockOn = false
target = nil
humanoid.AutoRotate = true
M2("On")
UIS.MouseIconEnabled = true
else
lockOn = true
target = FindNearestHRP(HRP)
humanoid.AutoRotate = false
M2("Off")
UIS.MouseIconEnabled = false
end
end

UIS.InputBegan:Connect(function(input,gpe)
if gpe then return end
if input.KeyCode == Enum.KeyCode.F and input.UserInputType == Enum.UserInputType.Keyboard then
if lockOn then
lockOn = false
target = nil
humanoid.AutoRotate = true
M2("On")
UIS.MouseIconEnabled = true
else
lockOn = true
target = FindNearestHRP(HRP)
humanoid.AutoRotate = false
M2("Off")
UIS.MouseIconEnabled = false
end
end
end)
runService.RenderStepped:Connect(function()
if target and lockOn then
local Mag = (HRP.Position- target.Position).Magnitude
if Mag <= 100 then
UIS.MouseBehavior = Enum.MouseBehavior.Default
cam.CFrame = CFrame.lookAt(cam.CFrame.Position,target.Position) * CFrame.new(3,1,0)
local direction = (HRP.Position - target.Position).Unit * Vector3.new(-1,0,-1)
HRP.CFrame =CFrame.lookAt(HRP.Position,HRP.Position + direction)
else
target = nil
lockOn = false
humanoid.AutoRotate = true
M2("On")
UIS.MouseIconEnabled = true
end
end
end)
contextActionService:BindAction("MobileLockOn",OnMobilePress,true)
contextActionService:SetPosition("MobileLockOn",UDim2.new(0,40,0,150))
contextActionService:SetTitle("MobileLockOn","F")

but i don’t really know if it’s what you are asking for :thinking: .

I Appreciate your try but it isnt what i want, i need a camera like the example

you could try change the FOV and use the Lerping to make it even smoother?