We use cookies on this site to enhance your user experience

Sep 11 2018, 9:56 AM 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 onTouched(other)
	if other.Name == "HumanoidRootPart" or other.Name == "Blaster" then
		local character = other.Parent
		character.Parent = game:GetService("ReplicatedStorage")
		
		local explosion = Instance.new("Explosion", workspace)
		explosion.Position = character:GetPrimaryPartCFrame().p
		
		wait(RESPAWN_DURATION)
		
		-- Reveal the character after they are knocked out and stop their movement
		character.Parent = workspace
		character.HumanoidRootPart.Velocity = Vector3.new(0,0,0)
		character.HumanoidRootPart.VectorForce.Force = Vector3.new(0,0,0)
	end
end

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)
		
		enemy.Touched:Connect(onTouched)
	end
end

spawnEnemies(ENEMY_COUNT)