How to spawn in more enemies?

Hello, I want to know how I could spawn in random enemies. I am making a Nextbot game just for fun but I want to know how I could spawn more every 5 or 10 seconds, then I want them to stop spawning at a max limit like 5 or so, so the game doesn’t have any issues, then when the game ends they all despawn and it just keeps going.

How would I do this? I was thinking of using a for loop or a while wait loop, but I wan’t sure how I would put this.

Here is the entire round script, I was thinking of putting it in the round section, since it would only happen when the round is playing.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Serverstorage = game:GetService("ServerStorage")

local TweenService = game:GetService("TweenService")

-- /// Tables

local votes = {}
local voted = {}

-- /// Map Voting Functions

local function highestVote()
	local map, highest = nil, 0
	
	for i, v in pairs(votes) do
		if v >= highest then
			map = i
			highest = v
		end
	end
	
	return map
end

local function voteMaps()
	
	local Status = ReplicatedStorage.RoundSystem:WaitForChild("Status")
	
	votes = {}
	voted = {}
	
	local maps = {}
	local mapsSelection = ReplicatedStorage.Maps:GetChildren()
	
	repeat
		local map = mapsSelection[math.random(1, #mapsSelection)]
		if not table.find(maps, map.Name) then
			table.insert(maps, map.Name)
			votes[map.Name] = 0
		end
	until #maps == 2
	
	ReplicatedStorage.RemoteEvents.Vote:FireAllClients(maps)
	
	for i = 10, 0, -1 do
		Status.Value = i
		wait(1)
	end
	
	local selectedMap = ReplicatedStorage.Maps[highestVote()]:Clone()
	selectedMap.Parent = workspace.CurrentMap
	
	ReplicatedStorage.RemoteEvents.Vote:FireAllClients()
end

ReplicatedStorage.RemoteEvents.Vote.OnServerEvent:Connect(function(player, map)
	if votes[map] and not table.find(voted, player.Name) then
		table.insert(voted, player.Name)
		votes[map] += 1
		ReplicatedStorage.RemoteEvents.Update:FireAllClients(votes)
	end
end)

local function TimerForm(s)
	return string.format("%2i:%02i", s/60, s%60)
end

local function System()
	
	-- /// Variables
	
	local Status = ReplicatedStorage.RoundSystem:WaitForChild("Status")
	
	local ServerStorage = game:GetService("ServerStorage")
	
	local currentMap = workspace.CurrentMap
	local Event = ServerStorage:FindFirstChild("Events"):GetChildren()
	local Lobby = ServerStorage:FindFirstChild("Lobby"):Clone()
	local Music = game.Workspace.Music
	
	local IntermissionMusicFolder = Music.Intermission:GetChildren()
	local IntermissionMusic = IntermissionMusicFolder[math.random(1, #IntermissionMusicFolder)]
	
	local RoundMusicFolder = Music.RoundMusic:GetChildren()
	local RoundMusic = RoundMusicFolder[math.random(1, #RoundMusicFolder)]
	
	-- /// Game Loop
	
	while task.wait() do
		
		print("Intermission")
		
		IntermissionMusic:Play()
		
		for i = 20, 0, -1 do
			Status.Value = i
			wait(1)
		end
		
		Status.Value = "Vote for a Map!"
		print("Map Voting")
		
		-- /// Map Voting
		
		voteMaps()
		
		ReplicatedStorage.RemoteEvents.FadeFolder.FadeOut:FireAllClients()
		
		task.wait(0.3)
		
		for _,Player in pairs(game.Players:GetPlayers()) do
			workspace.Lobby:Destroy()
			Player:LoadCharacter()
		end
		
		local selectedMap = ReplicatedStorage.Maps[highestVote()]:Clone()
		
		ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
		
		for i = 5, 0, -1 do
			Status.Value = i
			wait(1)
		end
		
		IntermissionMusic:Stop()
		
		-- /// Event Selection
		
		Status.Value = "Selecting Starting Nextbot"
			
		local selectedEvent = Event[math.random(1, #Event)]:Clone()
			
		task.wait(1)
			
		Status.Value = selectedEvent.Name
		
		task.wait(2)
		
		-- /// Spawning Event
		
		local mapModel = selectedMap
		local eventSpawn = nil
		
		for _,v in pairs (mapModel:GetDescendants()) do
			if (v.Name == "EventSpawn") then
				print("Found Event Spawn Point")
				eventSpawn = v
				break
			end
		end

		if (eventSpawn == nil) then
			error("EventSpawn not found in map model")
		end
		
		print("Game Started")
		
		-- /// Teleporting Event
		
		selectedEvent.Parent = workspace.Events
		selectedEvent:PivotTo(eventSpawn.CFrame)
		
		local CurrentEvent = game.Workspace.Events
		
		-- /// Round
		
		RoundMusic:Play()
		
		for i = 120, 0, -1 do
			Status.Value = TimerForm(i)
			wait(1)
		end
			
		Status.Value = "Game has ended!"
		
		CurrentEvent:ClearAllChildren()
		
		RoundMusic:Stop()
		
		print("Game Ended")
		task.wait(2)
		
		ReplicatedStorage.RemoteEvents.FadeFolder.FadeOut:FireAllClients()
		
		task.wait(0.3)
		
		currentMap:ClearAllChildren()
			
		for _,Player in pairs(game.Players:GetPlayers()) do
			Lobby.Parent = workspace
			Player:LoadCharacter()
			
			ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
		end
	end
	
	-- /// Restart
	
end

spawn(System)

How can I do this, I would really like some answers because the game right now feels bland.

1 Like

maybe try adding somewhere a wait(however many seconds you want) then
add the code of how many you want spawned? i’m just guessing i don’t know server sided coding good lol

1 Like

A for I loop would be good in a task.spawn function so it doesn’t yield anything. You can stop the loop if it achieves to any number you want.

So what? Would I say task.spawn(function() and then add a limit or something? I never did a task.spawn function before

1 Like
task.spawn(function()
for i = 0,5,1 do
--code
if i == num then
--code
end
end
end)

It works for the first part, but I don’t know what I would do for when it reaches the max limit, after spawning a couple, it just spawns a whole bunch of them.

Also after the round ends, it keeps spawning them

to break the loop, you just need to add in “break”.

Thats what I did, but it keeps going and they keeps spawning in after the round ends, which isn’t suppoused to happen.

Here is what the line of code looks like:

		for i = 30, 0, -1 do
			Status.Value = TimerForm(i)
			wait(1)
			
			task.spawn(function()
				
				task.wait(10)
				
				for i = 0, 4, 1 do
					
					local NextbotClone = Event[math.random(1, #Event)]:Clone()
					
					NextbotClone.Parent = workspace.Events
					NextbotClone:PivotTo(eventSpawn.CFrame)
					
					if i == 4 then
						break
					end
				end
			end)
		end

you didnt even add a wait() inside of it?

try doing if i > 3 then maybe it’ll work

I tried it, still spawns too many

Now that I understand the script much more clearly, I can see that you’re having a loop that makes a task.spawn function 30 times, meaning it spawning 4 x 30 from the I loop ontop, I’d suggest to close the function when it is done by using “task.cancel”. Also suggesting to make a bool value when the game starts so when the round starts, the function plays.

How would I do this, I have already made a bool value named RoundValue and set it to false, when the round starts, the value is set to true.

I am not sure if I would have to make a function and use the task.spawn() function inside it, and then use the task.cancel(MyFunction) on it.


local spawnbotfunction = task.spawn(function()
--code
end)

task.cancel(spawnbotfunction)

It won’t work, it keeps on spawning them again, there are no errors this time though about the task.cancel, also the limit won’t work either.

I am still figuring this out, which is why I said the task.cancel had errors in the output, I was trying to do what you said earlier.

is the task.spawn function still inside the for i loop?

Yeah, I’m pretty sure, here is the script for the round part:

		-- /// Event Selection
			
		local selectedEvent = Event[math.random(1, #Event)]:Clone()
			
		Status.Value = selectedEvent.Name
		
		-- /// Spawning Event
		
		local mapModel = selectedMap
		local eventSpawn = nil
		
		for _,v in pairs (mapModel:GetDescendants()) do
			if (v.Name == "EventSpawn") then
				print("Found Event Spawn Point")
				eventSpawn = v
				break
			end
		end

		if (eventSpawn == nil) then
			error("EventSpawn not found in map model")
		end
		
		print("Game Started")
		
		-- /// Teleporting Event
		
		selectedEvent.Parent = workspace.Events
		selectedEvent:PivotTo(eventSpawn.CFrame)
		
		local CurrentEvent = game.Workspace.Events
		
		-- /// Round
		
		RoundMusic:Play()
		
		RoundValue = true
		
		for i = 120, 0, -1 do
			Status.Value = TimerForm(i)
			wait(1)
			
			local spawnbotfunction = task.spawn(function() -- /// W.I.P
				for i = 0, 4, 1 do

					local NextbotClone = Event[math.random(1, #Event)]:Clone()

					NextbotClone.Parent = workspace.Events
					NextbotClone:PivotTo(eventSpawn.CFrame)

				end
			end) -- /// W.I.P
		end
			
		Status.Value = "Game Over"
		
		CurrentEvent:ClearAllChildren()
		RoundValue = false
		
		RoundMusic:Stop()
		
		print("Game Ended")
		task.wait(2)
		
		ReplicatedStorage.RemoteEvents.FadeFolder.FadeOut:FireAllClients()
		
		task.wait(0.3)
		
		currentMap:ClearAllChildren()
			
		for _,Player in pairs(game.Players:GetPlayers()) do
			LobbyClone.Parent = workspace
			Player:LoadCharacter()
			
			ReplicatedStorage.RemoteEvents.FadeFolder.FadeIn:FireAllClients()
			
		end
	end
	
	-- /// Restart
	
end

spawn(System)