local function GetTargetList(Range)
local TargetList = {}
for i, e in pairs(Entities:GetChildren()) do
if e:IsA("Model") and e ~= Character then
local Vector, InViewport = Camera:WorldToScreenPoint(e.PrimaryPart.Position)
if InViewport == true then
local ray = Ray.new(Character.PrimaryPart.Position, e.PrimaryPart.Position - Character.PrimaryPart.Position)
local ignorelist = Entities:GetDescendants()
table.insert(ignorelist, Character)
local Part = workspace:FindPartOnRayWithIgnoreList(ray, ignorelist)
if (Character.PrimaryPart.Position - e.PrimaryPart.Position).Magnitude <= Range and not Part then
local Ang = Vector.X - CenterX
print(e)
table.insert(TargetList, {Angle = Ang, Entity = e})
end
end
end
end
return TargetList
end
local function SortList(List)
local Remainder = math.abs(List[1].Angle - 0)
local Target = List[1].Entity
local Count = 1
while Count <= #List do
if math.abs(List[Count].Angle - 0) < Remainder then
Target = List[Count].Entity
Remainder = math.abs(List[Count].Angle - 0)
end
Count = Count + 1
end
return Target
end
local function EstablishTarget(input)
local TargetList = GetTargetList(math.huge)
local LeftList = {}
local RightList = {}
if input == Enum.KeyCode.R then
local Target = SortList(TargetList)
CameraTarget = Target
TargetAcquired = true
elseif input == Enum.KeyCode.Z and TargetAcquired == true then
for _,v in pairs(TargetList) do
print(#TargetList)
print(v.Entity)
if v.Angle < 0 and v ~= CameraTarget then
table.insert(LeftList, v)
end
local Target = SortList(LeftList)
CameraTarget = Target
TargetAcquired = true
end
elseif input == Enum.KeyCode.X and TargetAcquired == true then
for _,v in pairs(TargetList) do
if v.Angle > 0 and v ~= CameraTarget then
table.insert(RightList, v)
end
local Target = SortList(RightList)
CameraTarget = Target
TargetAcquired = true
end
end
end
The above code has one problem, whenever I attempt to press Z while facing Target from the front or X while facing Target from the rear, EstablishTarget() doesn't work properly. when #TargetList is printed, it says three but when attempting to print v.Entity it only prints one name when there are supposed to be three unique ones. If someone could help me to make it go through the list properly, it'd be greatly appreciated.
