0% found this document useful (0 votes)
4 views3 pages

Message

This script implements a targeting system for a player in a game, allowing them to lock onto the nearest valid target within a specified field of view and distance. It checks for team affiliations to avoid locking onto teammates and updates the camera to focus on the target when locked on. The lock-on state can be toggled using the 'Z' key, and it automatically disengages if the target player dies.

Uploaded by

xvyf2t2v7q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Message

This script implements a targeting system for a player in a game, allowing them to lock onto the nearest valid target within a specified field of view and distance. It checks for team affiliations to avoid locking onto teammates and updates the camera to focus on the target when locked on. The lock-on state can be toggled using the 'Z' key, and it automatically disengages if the target player dies.

Uploaded by

xvyf2t2v7q
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

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)

You might also like