We use cookies on this site to enhance your user experience

Sep 14 2018, 5:06 PM PST

-- Randomly spawns the number of enemies set in ENEMY_COUNT

local ENEMY_COUNT = 100
local SAFE_ZONE_SIZE = 50
local MAX_ENEMY_SPEED = 20
local RESPAWN_DURATION = 3

local ServerStorage = game:GetService("ServerStorage")

local function spawnEnemies(count)
	local baseplateSize = workspace.Arena.Baseplate.Size

	-- Loops until there are the amount of enemies set in ENEMY_COUNT
	for i = 1, count do
		-- Makes a copy of EnemyBall
		local enemy = ServerStorage.Enemies.EnemyBall:Clone()
		enemy.Parent = workspace.Enemies
		
		-- Places EnemyBall randomly on the baseplate
		local randomX = math.random(baseplateSize.X / -2 - SAFE_ZONE_SIZE, baseplateSize.X / 2 + SAFE_ZONE_SIZE)
		local randomZ = math.random(baseplateSize.Z / -2 - SAFE_ZONE_SIZE, baseplateSize.Z / 2 + SAFE_ZONE_SIZE)
		enemy.Position = Vector3.new(randomX , 9, randomZ)
		
		-- Assigns a random velocity to EnemyBall
		local randomVX = math.random(-1 * MAX_ENEMY_SPEED, MAX_ENEMY_SPEED)
		local randomVZ = math.random(-1 * MAX_ENEMY_SPEED, MAX_ENEMY_SPEED)
		enemy.Velocity = Vector3.new(randomVX, 0, randomVZ)
	end
end

-- Have enemies spawn on the baseplate
spawnEnemies(ENEMY_COUNT)