follow.Event:Connect((require(enemyFunctions)).Follow)while true do local v={}for U,L in pairs(players:GetChildren())do if L.Character and(L.Character:FindFirstChild("\072\117\109\097\110\111\105\100")and L.Character.Humanoid.Health>0)then if L:FindFirstChild("\083\116\097\116\115")and(L.Stats:FindFirstChild("\067\117\114\114\101\110\116\065\114\101\097")and L.Stats.CurrentArea.Value)then local U=L.Stats.CurrentArea.Value:FindFirstChild("\069\110\101\109\105\101\115")if U then for U,L in pairs(U:GetChildren())do if not table.find(v,L)then table.insert(v,L)end end end end end end for v,U in pairs(v)do local L=(require(enemyFunctions)).FindClosestPlayer(U,true)if L then if L.Character then local v=L.Character:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if v then follow:Fire(U,v)end end elseif U:FindFirstChild("\083\116\097\116\115")and U.Stats:FindFirstChild("\079\114\105\103\105\110\097\108\080\111\115\105\116\105\111\110")then U.EnemyHumanoid:MoveTo(U.Stats.OriginalPosition.Value)end end wait()end
-- Enemy AI system
follow.Event:Connect(require(enemyFunctions).Follow)

while true do
	local enemiesToConsider = {}
	for _, player in pairs(players:GetChildren()) do -- Adds all enemies in loaded areas to the considered table
		if
			player.Character
			and player.Character:FindFirstChild("Humanoid")
			and player.Character.Humanoid.Health > 0
		then
			if
				player:FindFirstChild("Stats")
				and player.Stats:FindFirstChild("CurrentArea")
				and player.Stats.CurrentArea.Value
			then
				local enemiesFolder = player.Stats.CurrentArea.Value:FindFirstChild("Enemies")
				if enemiesFolder then
					for _, enemy in pairs(enemiesFolder:GetChildren()) do
						if not table.find(enemiesToConsider, enemy) then
							table.insert(enemiesToConsider, enemy)
						end
					end
				end
			end
		end
	end
	for _, enemy in pairs(enemiesToConsider) do
		local closestPlayer = require(enemyFunctions).FindClosestPlayer(enemy, true)
		if closestPlayer then
			if closestPlayer.Character then
				local rootPart = closestPlayer.Character:FindFirstChild("HumanoidRootPart")
				if rootPart then
					follow:Fire(enemy, rootPart)
				end
			end
		elseif enemy:FindFirstChild("Stats") and enemy.Stats:FindFirstChild("OriginalPosition") then
			enemy.EnemyHumanoid:MoveTo(enemy.Stats.OriginalPosition.Value)
		end
	end
	wait()
end
