local U=game:GetService("\080\108\097\121\101\114\115")local d=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local N=game:GetService("\067\111\108\108\101\099\116\105\111\110\083\101\114\118\105\099\101")local C=require(d.EnemyModule)local T=workspace.Button:FindFirstChild("\079\110\047\079\102\102\066\117\116\116\111\110")local I=T.Color local X=Color3.new(.7,.2,.4)local t=tick()local E=false local u=nil local c=Vector3.new(0,6,0)local e={}local function D(U)if U then local d=U.EnemyName.Value local N=U.CFrame+c local T=U.Respawn.Value local I=U.Animations:GetChildren()local X=C.createEnemy(d,N,I,T)X:start()table.insert(e,X)else warn("\079\110\101 \111\114 \109\111\114\101 \111\102 \116\104\101 \097\114\103\117\109\101\110\116\115 \116\111 \115\112\097\119\110\069\110\101\109\121 \097\114\101 \110\105\108")end end local function B()local U=N:GetTagged("\069\110\101\109\121\083\112\097\119\110")for U,d in pairs(U)do D(d)end end local function i()for U,d in ipairs(e)do d:destroy()end e={}end local function O()if E then T.Color=X else T.Color=I end end local function w()if E then i()E=false else B()E=true end O()end local L=T.Touched:Connect(function(d)local N=d:FindFirstAncestorOfClass("\077\111\100\101\108")local C=U:GetPlayerFromCharacter(N)if C then local U=tick()if U-t<1 then return end t=U w()end end)C.onEnemyDied:Connect(function(U,d)print("\069\110\101\109\121 \100\105\101\100\058",U,d)end)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")

local EnemyModule = require(ReplicatedStorage.EnemyModule)

local button = workspace.Button:FindFirstChild("On/OffButton")

local offColor = button.Color
local onColor = Color3.new(0.7, 0.2, 0.4)

local lastPressTime = tick()
local buttonActivated = false
local connection = nil

local SPAWN_OFFSET = Vector3.new(0, 6, 0)

local enemies = {}

local function spawnEnemy(enemySpawn)
	if enemySpawn then
		local enemyName = enemySpawn.EnemyName.Value
		local spawnLocation = enemySpawn.CFrame + SPAWN_OFFSET
		local enemyRespawn = enemySpawn.Respawn.Value
		local animations = enemySpawn.Animations:GetChildren()

		local enemy = EnemyModule.createEnemy(enemyName, spawnLocation, animations, enemyRespawn)
		enemy:start()
		--enemy:stop()

		table.insert(enemies, enemy)
	else
		warn("One or more of the arguments to spawnEnemy are nil")
	end
end

local function spawnEnemies()
	local enemySpawners = CollectionService:GetTagged("EnemySpawn")
	for _, enemySpawn in pairs(enemySpawners) do
		spawnEnemy(enemySpawn)
	end
end

local function cleanUpEnemies()
	for _, enemy in ipairs(enemies) do
		enemy:destroy()
	end
	enemies = {}
end

local function toggleButtonColor()
	if buttonActivated then
		button.Color = onColor
	else
		button.Color = offColor
	end
end

local function onCharacterTouch()
	if buttonActivated then
		-- Clean up enemies and resources
		cleanUpEnemies()
		buttonActivated = false
	else
		-- Spawn enemies
		spawnEnemies()
		buttonActivated = true
	end

	toggleButtonColor()
end

-- Connect button press event
local connection = button.Touched:Connect(function(otherPart)
	local ancestor = otherPart:FindFirstAncestorOfClass("Model")
	local player = Players:GetPlayerFromCharacter(ancestor)
	if player then
		local now = tick()
		if now - lastPressTime < 1 then
			return
		end

		lastPressTime = now
		onCharacterTouch()
	end
end)

-- Connect event for enemy death
EnemyModule.onEnemyDied:Connect(function(enemyName, hittingTarget)
	print("Enemy died:", enemyName, hittingTarget)
end)
