local isLockedOn = false
local localPlayer = game.Players.LocalPlayer
local currentTarget = nil
local function findValidTarget()
local camera = game.Workspace.CurrentCamera
local character = localPlayer.Character
if not character then return nil end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return nil end
local closestPlayer = nil
local shortestAngle = math.huge
local fieldOfView = math.rad(50)
local distanceRequirement = 1000
local redTeamExists = false
local blueTeamExists = false
for _, team in ipairs(game.Teams:GetTeams()) do
if team.Name == "Red Team" then
redTeamExists = true
elseif team.Name == "Blue Team" then
blueTeamExists = true
end
end
for _, player in ipairs(game.Players:GetPlayers()) do
if player ~= localPlayer and player.Character then
local targetCharacter = player.Character
local targetHumanoidRootPart =
targetCharacter:FindFirstChild("HumanoidRootPart")
if targetHumanoidRootPart then
local teamName = player.Team and player.Team.Name
if teamName and not (teamName == "Spectator" or teamName ==
"Spectators" or teamName == "AFK") then
if (redTeamExists or blueTeamExists) and teamName ~=
localPlayer.Team.Name then
local distance = (targetHumanoidRootPart.Position -
humanoidRootPart.Position).Magnitude
if distance <= distanceRequirement then
local targetDirection =
(targetHumanoidRootPart.Position - humanoidRootPart.Position).unit
local cameraDirection = camera.CFrame.LookVector
local angle =
math.acos(cameraDirection:Dot(targetDirection))
if angle < fieldOfView and angle < shortestAngle then
closestPlayer = player
shortestAngle = angle
end
end
elseif not redTeamExists and not blueTeamExists then
local distance = (targetHumanoidRootPart.Position -
humanoidRootPart.Position).Magnitude
if distance <= distanceRequirement then
local targetDirection =
(targetHumanoidRootPart.Position - humanoidRootPart.Position).unit
local cameraDirection = camera.CFrame.LookVector
local angle =
math.acos(cameraDirection:Dot(targetDirection))
if angle < fieldOfView and angle < shortestAngle then
closestPlayer = player
shortestAngle = angle
end
end
end
end
end
end
end
return closestPlayer
end
local function updateLockOn()
local camera = game.Workspace.CurrentCamera
if isLockedOn and currentTarget then
local targetPosition = currentTarget.Character.HumanoidRootPart.Position
local cameraPosition = camera.CFrame.Position
camera.CFrame = CFrame.new(cameraPosition, targetPosition)
end
end
local function onKeyPress(input)
if input.KeyCode == Enum.KeyCode.Z then
if isLockedOn then
isLockedOn = false
currentTarget = nil
else
local targetPlayer = findValidTarget()
if targetPlayer then
isLockedOn = true
currentTarget = targetPlayer
local targetHumanoid =
targetPlayer.Character:FindFirstChildOfClass("Humanoid")
if targetHumanoid then
targetHumanoid.Died:Connect(function()
isLockedOn = false
currentTarget = nil
end)
end
end
end
end
end
game:GetService("RunService").RenderStepped:Connect(function()
if isLockedOn and currentTarget then
local targetPlayer = currentTarget
if targetPlayer then
local camera = game.Workspace.CurrentCamera
local targetPosition = targetPlayer.Character.HumanoidRootPart.Position
local cameraPosition = camera.CFrame.Position
camera.CFrame = CFrame.new(cameraPosition, targetPosition)
else
isLockedOn = false
end
end
end)
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)