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.