local Players = game:GetService("Players") local RunService = game:GetService("RunService") local LocalPlayer = Players.LocalPlayer local Camera = workspace.CurrentCamera local MAX_DISTANCE = 500 local LOCK_ON = true local TEAM_CHECK = true local WALL_CHECK = true local CurrentTarget = nil local Highlight = nil local function isEnemy(player) if player == LocalPlayer then return false end if TEAM_CHECK and player.Team == LocalPlayer.Team then return false end return true end local function isVisible(character) if not WALL_CHECK then return true end local head = character:FindFirstChild("Head") if not head then return false end local origin = Camera.CFrame.Position local direction = head.Position - origin local params = RaycastParams.new() params.FilterType = Enum.RaycastFilterType.Exclude params.FilterDescendantsInstances = { LocalPlayer.Character, character } local result = workspace:Raycast(origin, direction, params) return result == nil end local function getNearestEnemy() local nearest = nil local nearestDistance = MAX_DISTANCE local myCharacter = LocalPlayer.Character local myRoot = myCharacter and myCharacter:FindFirstChild("HumanoidRootPart") if not myRoot then return nil end for _, player in ipairs(Players:GetPlayers()) do if isEnemy(player) then local character = player.Character local humanoid = character and character:FindFirstChildOfClass("Humanoid") local head = character and character:FindFirstChild("Head") if humanoid and head and humanoid.Health > 0 then local distance = (head.Position - myRoot.Position).Magnitude if distance < nearestDistance and isVisible(character) then nearest = player nearestDistance = distance end end end end return nearest end local function removeHighlight() if Highlight then Highlight:Destroy() Highlight = nil end end local function highlightTarget(player) removeHighlight() if not player or not player.Character then return end Highlight = Instance.new("Highlight") Highlight.Name = "LockOnHighlight" Highlight.Adornee = player.Character Highlight.FillTransparency = 0.5 Highlight.OutlineTransparency = 0 Highlight.Parent = player.Character end local function setTarget(player) if CurrentTarget == player then return end CurrentTarget = player highlightTarget(player) end local function clearTarget() CurrentTarget = nil removeHighlight() end RunService.RenderStepped:Connect(function() if not LOCK_ON then clearTarget() return end if not CurrentTarget then setTarget(getNearestEnemy()) end if CurrentTarget then local character = CurrentTarget.Character local humanoid = character and character:FindFirstChildOfClass("Humanoid") local head = character and character:FindFirstChild("Head") if not character or not humanoid or humanoid.Health <= 0 or not head or not isEnemy(CurrentTarget) or not isVisible(character) then clearTarget() setTarget(getNearestEnemy()) else -- Aim directly at the target's Head Camera.CFrame = CFrame.lookAt( Camera.CFrame.Position, head.Position ) end end end)