LockOn Camera gone wrong

Hello, people of the forum. I have a problem.

I’m trying to make a lock on script using this YouTube tutorial,
Roblox Studio | How To Make Lock On System

But I seem to be having a problem. This is my 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 = game.Workspace.CurrentCamera
local lockon = false

local Target

function FindNearestTarget(HRP)
	local TargetDistance = 50
	local Target
	
	for i,v in pairs(workspace:GetDescendants()) do
		if v:IsA("Model") and v:FindFirstChild("HumanoidRootPart") 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

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

UIS.InputBegan:Connect(function(key, IsTyping)
	if IsTyping then return end
	
	if key.KeyCode == Enum.KeyCode.G then
		if lockon then
			lockon = false
			Target = nil
			Humanoid.AutoRotate = true
			M2("On")
			UIS.MouseIconEnabled = true
		else
			lockon = true
			Target =FindNearestTarget(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 < 50 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,-.15,-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)

And this is what happenes

Idk what I did wrong, but I don’t think that shouldn’t be happening. Does anyone know how to fix this?

1 Like

According to the comments try changing -.15 to 0

I already tried that, and it still does the same thing.
Thanks anyway

Try changing camera CFrame position to humanoid rootpart CFrame

Now the screen goes black.
I still don’t know what’s wrong. The code I’m using could be outdated.