Tower randomly stops shooting

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want the tower to constantly shoot.

  1. What is the issue? Include screenshots / videos if possible!

It randomly stops shooting or takes a few seconds before it starts shooting.

function module.Attack(tower, priority)
	local TouchingParts = workspace:GetPartsInPart(tower:WaitForChild('Range'))
	
	local function Target()
		local EnemyTable = {}
		
		if game.ReplicatedStorage:WaitForChild('Started').Value == false then
			return
		end
		
		for i,v in TouchingParts do
			local FoundParent = v.Parent

			if FoundParent ~= tower and not FoundParent:IsDescendantOf(workspace:WaitForChild('Towers')) then
				local FoundHumanoid = FoundParent:FindFirstChild('Humanoid')
				local Player = game.Players:GetPlayerFromCharacter(FoundParent)
				
				if FoundHumanoid and not Player then
					local RootPart = FoundParent:WaitForChild('HumanoidRootPart')
					local Base = workspace:WaitForChild('Desert'):WaitForChild('Base')
					local Magnitude = (RootPart.Position - Base.Position).Magnitude
					table.insert(EnemyTable, Magnitude)
					
					table.sort(EnemyTable, function(a,b)
						return a < b -- sorting lower to higher
					end)
					
					local FirstEnemy = EnemyTable[1]
					
					local loweredPriority = string.lower(priority)
					
					if loweredPriority == 'first' then
						for i, Enemy in workspace:WaitForChild('Enemies'):GetChildren() do 
							local HRP = Enemy:WaitForChild('HumanoidRootPart')
							local EnemyMagnitude = (HRP.Position - Base.Position).Magnitude

							if EnemyMagnitude <= FirstEnemy then
								module.HandleEffects(tower)
								Enemy:WaitForChild('Humanoid'):TakeDamage(tower:GetAttribute('Damage'))
								break
							end
						end
					elseif loweredPriority == 'last' then
						for i, Enemy in workspace:WaitForChild('Enemies'):GetChildren() do 
							local HRP = Enemy:WaitForChild('HumanoidRootPart')
							local EnemyMagnitude = (HRP.Position - Base.Position).Magnitude

							if EnemyMagnitude <= EnemyTable[#EnemyTable] then
								module.HandleEffects(tower)
								Enemy:WaitForChild('Humanoid'):TakeDamage(tower:GetAttribute('Damage'))
								break
							end
						end
					elseif loweredPriority == 'close' then
						table.clear(EnemyTable)
						
						for i, Enemy in workspace:WaitForChild('Enemies'):GetChildren() do 
							local HRP = Enemy:WaitForChild('HumanoidRootPart')
							local EnemyMagnitude = (HRP.Position - tower:WaitForChild('HumanoidRootPart').Position).Magnitude

							table.insert(EnemyTable, EnemyMagnitude)
						end
						
						table.sort(EnemyTable, function(a,b)
							return a < b -- sorting lower to higher
						end)
						
						for i, Enemy in workspace:WaitForChild('Enemies'):GetChildren() do 
							local HRP = Enemy:WaitForChild('HumanoidRootPart')
							local EnemyMagnitude = (HRP.Position - Base.Position).Magnitude

							if EnemyMagnitude <= EnemyTable[1] then
								module.HandleEffects(tower)
								Enemy:WaitForChild('Humanoid'):TakeDamage(tower:GetAttribute('Damage'))
								break
							end
						end
					end					
				end
			end
		end	
	end
	
	if tower.Name == 'Cone Man Sniper L' then
		Target()
	end
end
local TowerModule = require(game.ReplicatedStorage:WaitForChild('Modules'):WaitForChild('TowerModule'))
local new_tower = script.Parent
local CD = new_tower:GetAttribute('CD')

while true do
	if CD <= 0 then
		CD = 1
	end
	TowerModule.Attack(new_tower, 'first')
	task.wait(CD)
end