CCCPaste

Player X AI Script Fixed 2

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local Teams = game:GetService("Teams")

local AWFolder = ReplicatedStorage:WaitForChild("AW Tacticals")
local SoHFolder = ReplicatedStorage:WaitForChild("SoH Tacticals")

local spawnSets = {
	A = {
		AW = Workspace:WaitForChild("AWSpawnsA"),
		SoH = Workspace:WaitForChild("SoHSpawnsA"),
		Players = Workspace:WaitForChild("PlayerTeamSpawns"),
	},
	B = {
		AW = Workspace:WaitForChild("AWSpawnsB"),
		SoH = Workspace:WaitForChild("SoHSpawnsB"),
		Players = Workspace:WaitForChild("PlayerTeamSpawns"),
	},
	C = {
		AW = Workspace:WaitForChild("AWSpawnsC"),
		SoH = Workspace:WaitForChild("SoHSpawnsC"),
		Players = Workspace:WaitForChild("PlayerTeamSpawns"),
	},
}

local RESPAWN_INTERVAL = 10
local MIN_AW = 10
local MIN_SOH = 10
local currentPoint = "A"

local function getAliveCount(namePattern)
	local count = 0
	for _, npc in ipairs(Workspace:GetDescendants()) do
		if npc:IsA("Model") and npc:FindFirstChild("Humanoid") and npc.Name:match(namePattern) then
			count += 1
		end
	end
	return count
end

local function getRandomTemplate(folder)
	local models = folder:GetChildren()
	if #models == 0 then return nil end
	return models[math.random(1, #models)]
end

local function spawnNPC(folder, templateFolder, namePattern, numToSpawn)
	local spawnPoints = folder:GetChildren()
	if #spawnPoints == 0 then return end

	for i = 1, numToSpawn do
		local template = getRandomTemplate(templateFolder)
		local spawnPoint = spawnPoints[math.random(1, #spawnPoints)]
		if template and spawnPoint then
			local clone = template:Clone()
			if clone.PrimaryPart then
				clone:SetPrimaryPartCFrame(spawnPoint.CFrame + Vector3.new(0, 2, 0))
				clone.Parent = Workspace
			end
		end
	end
end

local function updatePlayerSpawns(pointKey)
	local playerSpawns = Workspace:WaitForChild("PlayerTeamSpawns")
	
	print("Updating player spawns to point:", pointKey)

	for _, folder in ipairs(playerSpawns:GetChildren()) do
		if folder:IsA("Folder") then
			local isActive = folder.Name:sub(-1) == pointKey
			
			print(string.format("  Folder: %s, Active: %s", folder.Name, tostring(isActive)))
			
			for _, spawn in ipairs(folder:GetChildren()) do
				if spawn:IsA("SpawnLocation") then
					spawn.Enabled = isActive
				end
			end
		end
	end
end

local function setActivePoint(pointKey)
	if not spawnSets[pointKey] then
		warn("Invalid control point:", pointKey)
		return
	end
	currentPoint = pointKey
	print("Active point is now:", pointKey)
	updatePlayerSpawns(pointKey)
end

task.spawn(function()
	while true do
		local awCount = getAliveCount("Arctic Wolf")
		local sohCount = getAliveCount("SonsOfHorus")

		local active = spawnSets[currentPoint]

		if awCount < MIN_AW then
			spawnNPC(active.AW, AWFolder, "Arctic Wolf", MIN_AW - awCount)
		end
		if sohCount < MIN_SOH then
			spawnNPC(active.SoH, SoHFolder, "SonsOfHorus", MIN_SOH - sohCount)
		end

		task.wait(RESPAWN_INTERVAL)
	end
end)

local PointChangedEvent = ReplicatedStorage:WaitForChild("PointChanged")

PointChangedEvent.Event:Connect(function(pointKey)
	print("PointChanged fired with:", pointKey)
	setActivePoint(pointKey)
end)

setActivePoint("A")