Lock-on script not working properly!

I want to make a script where you lock on to the nearest player while holding LeftShift.

The problem is that it does not lock on to the enemy, which means it doesn’t find the enemy. I think that mabe the magnitude does not work since it does not print 1 on line 50.

I don’t know why it does not find the target, since in my other scripts it works.

local player = game.Players.LocalPlayer
local char = player.Character
local Humanoid = char:WaitForChild("Humanoid")

PlayerHRP = char.HumanoidRootPart

local Cam = game.Workspace.CurrentCamera
local run = game:GetService("RunService")
local X = 12
local Focused = false
	
local input = game:GetService("UserInputService")

 
input.InputBegan:connect(function(k)
    local key = k.KeyCode
    if key == Enum.KeyCode.LeftShift then
        Focused = true
		Cam.CameraType = "Scriptable"
    end
end)
 
input.InputEnded:connect(function(k)
    local key = k.KeyCode
    if key == Enum.KeyCode.LeftShift then
        Focused = false
		Cam.CameraType = "Custom"
    end
end)

local function findTarget()
	local dist = 100
	local Target = nil
		for i,v in pairs(game.Workspace:GetChildren()) do
			local human = v:FindFirstChild("Humanoid")
			local TargetHRP = v:FindFirstChild("HumanoidRootPart")
				if TargetHRP and human and PlayerHRP and v ~= script.Parent.Parent then
					if (PlayerHRP.Position - TargetHRP.Position).magnitude < dist then
						dist = (PlayerHRP.Position - TargetHRP.Position).magnitude
						Target = TargetHRP
					end
				end
			end
			return Target
		end


local TargetHPR = findTarget()
	if TargetHPR then
		   print(1)
		   while Focused do
		   wait()
		   while(TargetHPR.Position - PlayerHRP.Position).magnitude > 10 and TargetHPR.Parent.Humanoid.Health > 0 do
				Health = TargetHPR.Parent.Head:FindFirstChild("BillboardGui")
				Health.Enabled = true
				local B = TargetHPR.Position
				local A = PlayerHRP.Position
				local Offset = Vector3.new(5,1,0)
				local V = B + ((A + Offset) - B).Unit * ((A - B).Magnitude + X)
				Cam.CFrame = CFrame.new(V, B)
				run.RenderStepped:Wait()
			end
		end
end

local human = v:FindFirstChild(“Humanoid”)
local TargetHRP = v:FindFirstChild(“HumanoidRootPart”)
if TargetHRP and human and PlayerHRP and v ~= script.Parent.Parent then

change that to

local human = v:FindFirstChild(“Humanoid”)
local TargetHRP = human and human:FindFirstChild(“HumanoidRootPart”)
if TargetHRP and PlayerHRP and v ~= script.Parent.Parent then

1 Like

Your issue is that you only check for nearby players once. You need to do it every time left shift is pressed. I’ve edited your code so that it does that, as well as made it disable the Health tag of the previous target if it’s switched/lost:

local player = game.Players.LocalPlayer
local char = player.Character
local Humanoid = char:WaitForChild("Humanoid")

PlayerHRP = char.HumanoidRootPart

local Cam = game.Workspace.CurrentCamera
local run = game:GetService("RunService")
local X = 12
local Focused = false

local function findTarget()
	local dist = 100
	local Target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local TargetHRP = v:FindFirstChild("HumanoidRootPart")
		if TargetHRP and human and PlayerHRP and v ~= script.Parent.Parent then
			if (PlayerHRP.Position - TargetHRP.Position).magnitude < dist then
				dist = (PlayerHRP.Position - TargetHRP.Position).magnitude
				Target = TargetHRP
			end
		end
	end
	return Target
end

local LastMark
local function FaceTarget(TargetHRP)
	local Health = TargetHRP.Parent.Head:FindFirstChild("BillboardGui")
	Health.Enabled = true
	if LastMark and LastMark ~= Health then
		LastMark.Enabled = false --disable the billboard gui when focus is changed
	end
	LastMark = Health
	
	local B = TargetHRP.Position
	local A = PlayerHRP.Position
	local Offset = Vector3.new(5,1,0)
	local V = B + ((A + Offset) - B).Unit * ((A - B).Magnitude + X)
	Cam.CFrame = CFrame.new(V, B)
	run.RenderStepped:Wait()
end
	
local input = game:GetService("UserInputService")
input.InputBegan:connect(function(k)
    local key = k.KeyCode
    if key == Enum.KeyCode.LeftShift then
        Focused = true
		Cam.CameraType = "Scriptable"
		while Focused do
			local TargetHRP = findTarget()
			if TargetHRP then
				FaceTarget(TargetHRP)
			end
			wait()
		end
		LastMark.Enabled = false --disable the billboard gui when focus is lost
    end
end)
 
input.InputEnded:connect(function(k)
    local key = k.KeyCode
    if key == Enum.KeyCode.LeftShift then
        Focused = false
		Cam.CameraType = "Custom"
    end
end)

(untested, forgive any errors)

A few tipes while at it:

  • Use Connect instead of connect
  • Don’t use global variables (the ones without local before them)
  • Indent your code correctly, easier to read
1 Like

Thank you, it works now although the camera is a bit glitched when walking but I think I can fix that.

Another question, while moving, my player kinda bugs around. The camera is like lagging. Why is that? Could not find the cause.

https://gyazo.com/e3b05217ef310426982ad4206b55076a