I'm trying out a tower defence game. The way my towers worked was that they attacked the zombies that were closest to the end by body position. I decided to change it to attack the first unit but there's a problem. I couldn't find out how to get the zombies to attack the first unit. I found on the Dev Forum on a simple code for that sort of thing but it didn't work as expected. The towers did attack the first unit but they would lock onto that unit and all the same types of enemies (If it targets boss, it'll target and kill all the other bosses and then target other units) and even if they target the first unit if a faster enemy passes it, it doesn't target that zombie. I tried removing the unit from a table after the action is fired but as a result, it attacked an enemy then targetted a different one. Any suggestions?

This is the whole Tower Script:

local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game.Lighting.Towers

local Shoot = true

local Script = script
local Model = script.Parent.Parent

local Map = Workspace:WaitForChild("Map")

local Torso = Model:WaitForChild("Torso")
local Head = Model:WaitForChild("Head")
local Humanoid = Model:WaitForChild("Humanoid")
local Path = Map:WaitForChild("DefaultPath")
local ReversedPath = Map:WaitForChild("ReversedPath")

local Handles = {}

wait()
Model.Parent = workspace.Towers
Model.NoPlaceArea.ManualWeld:Destroy()
Model.NoPlaceArea.Anchored = true
if Model:FindFirstChild("CliffTower") == nil then
Model.Torso.Anchored = true
end
for _, v in pairs(Model:GetChildren()) do
	if v:FindFirstChild("Output") then
		table.insert(Handles, v.Name)
	end
end

local Configuration = Model:WaitForChild("Configuration")
local _Damage = Configuration:WaitForChild("Damage")
local _Range = Configuration:WaitForChild("Range")
local _Can = Configuration:WaitForChild("Can")
local _Hidden = Configuration:WaitForChild("CanAttackHiddens")
local _ReloadTime = Configuration:WaitForChild("ReloadTime")
local _IsAttacker = Configuration:WaitForChild("IsAttacker")
local _Owner = Configuration:WaitForChild("Owner")
local _Level = Configuration:WaitForChild("Level")

local Enemies = workspace:WaitForChild("Enemies")
local NearbyEnemies = {}

function getHandle()
	for _, v in pairs(Handles) do
		local obj = Model:WaitForChild(v)
		wait()
		if obj then
			return obj
		end
	end
end

function shootTarget(Target)
	if Target then
		Torso.CFrame = CFrame.new(Torso.Position, Vector3.new(Target["Torso"].Position.X, Torso.Position.Y, Target["Torso"].Position.Z))
			spawn(function()
				local a = math.random(1,10)
				if Target:FindFirstChild("Zombie") then
					Target.Zombie:TakeDamage(_Damage.Value)
				end
				getHandle():WaitForChild("Output")["FireSound"..a]:Play()
				if getHandle():WaitForChild("Output"):FindFirstChild("Reload") ~= nil then
				wait(0.5)
				getHandle():WaitForChild("Output")["Reload"]:Play()
				end
			end)
			if Players[_Owner.Value]:FindFirstChild("Currency") and Target.Zombie.Health >= script.Parent.Parent:WaitForChild("Configuration").Damage.Value and Target:FindFirstChild("Summoned") == nil then
			Players[_Owner.Value]["Currency"].Value = Players[_Owner.Value]["Currency"].Value + script.Parent.Parent:WaitForChild("Configuration").Damage.Value
			elseif Target.Zombie.Health <= (script.Parent.Parent:WaitForChild("Configuration").Damage.Value - 1) then
			Players[_Owner.Value]["Currency"].Value = Players[_Owner.Value]["Currency"].Value + Target.Zombie.Health
			end
	end
end

function GetHumanoids()
	local Enemies = {}
	
	if script.Parent.Parent.Targets.Value == "Closest to Base" then
	for i,x in pairs(workspace.Enemies:GetChildren()) do
	if x:FindFirstChild("Zombie") and x.Zombie.Health ~= 0 and x.Configuration.Hidden.Value == false and (x.Torso.Position - script.Parent.Torso.Position).magnitude <= _Range.Value / 2 then
	table.insert(Enemies, (workspace.Map.BlueFinish.Position - x.Torso.Position).magnitude)
	end
	end
	
	table.sort(Enemies)
	
	for i,x in pairs(Enemies) do
		if i ~= 1 then
			table.remove(Enemies,i)
		end
	end
	
	for i,x in pairs(workspace.Enemies:GetChildren()) do
		if x:FindFirstChild("Zombie") and x.Zombie.Health ~= 0 and x.Configuration.Hidden.Value == false then
			if (workspace.Map.BlueFinish.Position - x.Torso.Position).magnitude == Enemies[1] then
				return x
			end
		end
	end
	elseif script.Parent.Parent.Targets.Value == "Closest to Tower" then
	for i,x in pairs(workspace.Enemies:GetChildren()) do
	if x:FindFirstChild("Zombie") and x.Zombie.Health ~= 0 and x.Configuration.Hidden.Value == false and (x.Torso.Position - script.Parent.Torso.Position).magnitude <= _Range.Value / 2 then
	table.insert(Enemies, (script.Parent.Torso.Position - x.Torso.Position).magnitude)
	end
	end
	
	table.sort(Enemies)
	
	for i,x in pairs(Enemies) do
		if i ~= 1 then
			table.remove(Enemies,i)
		end
	end
	
	for i,x in pairs(workspace.Enemies:GetChildren()) do
		if x:FindFirstChild("Zombie") and x.Zombie.Health ~= 0 and x.Configuration.Hidden.Value == false then
			if (script.Parent.Torso.Position - x.Torso.Position).magnitude == Enemies[1] then
				return x
			end
		end
	end
elseif script.Parent.Parent.Targets.Value == "First" then

for i, enemy in pairs (workspace.Enemies:GetChildren()) do
     if (script.Parent.Torso.Position - enemy.Torso.Position).magnitude < _Range.Value / 2 and enemy.Configuration.Hidden.Value == false then
         table.insert(NearbyEnemies, enemy)
     end
end
	return (NearbyEnemies[1])
	end
end


spawn(function()
	while wait(_ReloadTime.Value) do
		shootTarget(GetHumanoids())
	end
end)

if you are able to change up the function for the "Target First unit" function, that'd be fine.