Wanting 15 seconds delay before round starts doesnt work (Solved)

So I’m making a tower defense game, but when I’m trying to make the gamemodes like Easy, Medium, Hard and Extreme to pop up, it works and then after 15 seconds they disappear, but the problem I’m facing is that the round already starts when they’re voting, and I’ve tried a lot to change it but can’t fix it.

This is my main script in serverscriptservice

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local mob = require(script.Mob)
local tower = require(script.Tower)
local round = require(script.Round)
local map = workspace.City

local info = workspace.Info
local gameOver = false

map.Base.Humanoid.HealthChanged:Connect(function(health)
	if health <= 0 then
		gameOver = true
		info.Message.Value = "GAME OVER"
	end
end)

for i=3, 0, -1 do
	info.Message.Value = "Game starting in..." .. i
	task.wait(1)
end

for wave=1, 20 do
	info.Wave.Value = wave
	info.Message.Value = ""
	
	round.GetWave(wave, map)
	
	repeat
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or gameOver
	
	if not gameOver and wave == 20 then
		info.Message.Value = "VICTORY"
	elseif not gameOver then
		local reward = 50 * wave
		for i, player in ipairs(Players:GetPlayers()) do
			player.leaderstats.Cash.Value += reward
		end
		info.Message.Value = "Wave Reward: $" .. reward
		task.wait(2)

		for i=5, 0, -1 do
			info.Message.Value = "Next wave starting in..." .. i
			task.wait(1)
		end
	else
		break
	end
end

this is my mob modulescript in the main script

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

local Events = ReplicatedStorage:WaitForChild("Events")
local VoteForGameMode = Events:WaitForChild("VoteForGameMode")

local zombieSpawnEnabled = false

function SetZombiesSpawnEnabled(enabled)
	zombieSpawnEnabled = enabled
end

local function EnableZombieSpawningAfterDelay()
	wait(15)  -- Wait for 15 seconds
	SetZombiesSpawnEnabled(true)  -- Enable zombie spawning after delay
end

local mob = {}

function mob.Move(mob, map)
	local humanoid = mob:WaitForChild("Humanoid")
	local waypoints = map.Waypoints:GetChildren()

	for waypointIndex, waypoint in ipairs(waypoints) do
		mob.MovingTo.Value = waypointIndex
		humanoid:MoveTo(waypoint.Position)
		humanoid.MoveToFinished:Wait()
	end

	mob:Destroy()

	map.Base.Humanoid:TakeDamage(humanoid.Health)
end

function mob.Spawn(name, quantity, map)
	if zombieSpawnEnabled then
		local mobExists = ServerStorage.Mobs:FindFirstChild(name)

		if mobExists then
			for i = 1, quantity do
				task.wait(0.5)
				local newMob = mobExists:Clone()
				newMob.HumanoidRootPart.CFrame = map.Start.CFrame
				newMob.Parent = workspace.Mobs
				newMob.HumanoidRootPart:SetNetworkOwner(nil)

				local movingTo = Instance.new("IntValue")
				movingTo.Name = "MovingTo"
				movingTo.Parent = newMob

				for _, object in ipairs(newMob:GetDescendants()) do
					if object:IsA("BasePart") then
						object.CollisionGroup = "Mob"
					end
				end

				newMob.Humanoid.Died:Connect(function()
					task.wait(0.5)
					newMob:Destroy()
				end)

				coroutine.wrap(mob.Move)(newMob, map)
			end
		else
			warn("Requested mob doesn't exist:", name)
		end
	end
end

VoteForGameMode.OnServerEvent:Connect(function(player, gameMode)
	SetZombiesSpawnEnabled(false)  -- Disable zombie spawning when voting starts
	spawn(EnableZombieSpawningAfterDelay)  -- Enable zombie spawning after 15 seconds
end)

return mob

and this is my round modulescript in the main script

local mob = require(script.Parent.Mob)
local round = {}

function round.GetWave(wave, map)
	if wave == 1 then
		mob.Spawn("Zombie", 5,  map)
	elseif wave == 2 then
		mob.Spawn("Bendy", 4,  map)
		mob.Spawn("Zombie", 6,  map)
	elseif wave == 3 then
		mob.Spawn("Peter", 4,  map)
		mob.Spawn("Zombie", 5,  map)
		mob.Spawn("Bendy", 6,  map)
	elseif wave == 4 then
		mob.Spawn("Zombie", 5,  map)
		mob.Spawn("Bendy", 5,  map)
		mob.Spawn("Peter", 4,  map)
	elseif wave == 5 then
		mob.Spawn("Zombie", 5,  map)
		mob.Spawn("Bendy", 7,  map)
		mob.Spawn("Peter", 6,  map)
		mob.Spawn("Cartoon Duck", 2,  map)
	elseif wave == 6 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 7 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 8 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 9 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 10 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 11 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 12 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 13 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 14 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 15 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 16 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 17 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 18 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 19 then
		mob.Spawn("Zombie", 25,  map)
	elseif wave == 20 then
		mob.Spawn("Zombie", 25,  map)
	end
end

return round

Please help

2 Likes

Please use task.wait before I slap you, thanks

1 Like

please provide a more elaborate explanation of your problem, where in your code is there an issue etc. and also show the error code you are receiving.

I already explained all of that, also there is no error code, it just doesn’t work.

Sorry :pray:

erwreteryrtiuytyreouyoreuyothrdfiughrdkhgilfdhh

You could just check if the current wave is 1 and if it is, yield the code for 15 seconds using task.wait():

for wave=1, 20 do
    if wave == 1 then task.wait(15) end -- Yields for 15 seconds
	info.Wave.Value = wave
	info.Message.Value = ""
	
	round.GetWave(wave, map)
	
	repeat
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or gameOver
	
	if not gameOver and wave == 20 then
		info.Message.Value = "VICTORY"
	elseif not gameOver then
		local reward = 50 * wave
		for i, player in ipairs(Players:GetPlayers()) do
			player.leaderstats.Cash.Value += reward
		end
		info.Message.Value = "Wave Reward: $" .. reward
		task.wait(2)

		for i=5, 0, -1 do
			info.Message.Value = "Next wave starting in..." .. i
			task.wait(1)
		end
	else
		break
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.