I’m trying to create a system that spawns random events every few seconds, and when a new event starts the one before him ends, for example: a meteor shower event ends right when a zombie apocalypse event starts.
The code:
local eventTime = 15
local timer = 0
local events = {
{
name = "Meteor Shower",
f = function()
local spawns = game.Workspace.SkyEventSpawns:GetChildren()
repeat
while wait(0.5) do
local randomPos = spawns[math.random(1, #spawns)]
local meteorClone = game.ReplicatedStorage.Meteor:Clone()
meteorClone.Parent = workspace
meteorClone.PrimaryPart.Position = randomPos.Position
end
until wait(eventTime)
end
},
{
name = "Zombie Apocalypse",
f = function()
local spawns = game.Workspace.Targets:GetChildren()
for _, spawnPoint in pairs(spawns) do
local spawnPos = spawnPoint.Position
local zombie = game.ReplicatedStorage.Zombie:Clone()
zombie.Parent = workspace
zombie:WaitForChild("Charcter"):FindFirstChild("HumanoidRootPart").CFrame.Position = spawnPos
end
end
}
}
local runEvent = runService.Heartbeat:Connect(function(dt)
if isInRound.Value == true then
timer += dt
if timer > eventTime then
timer = timer % eventTime
local event_index = math.random(#events)
local event = events[event_index]
local event_name = event.name
local event_f = event.f
print("There is a " .. event_name)
event_f()
end
end
end)
The issues I have are that the events don’t stop when another one starts, and the zombie apocalypse event only spawns one zombie instead of a zombie for each spawn point.