Why can't it count down?

hello,

so for my game, there are rounds… so i wanted to make a round system which plays infinitly and just plays every round automatically.
but for some reason, the first countdown in the script, ends at 1 and stays at 1 , for the rest of the game!
i don’t know why because i’m getting Zero errors and still it doesn’t work.

here is the script which implements the module:

local RoundModule = require(game.ReplicatedStorage.RoundModule)
local Raid = game.ReplicatedStorage:WaitForChild("ChangeRaid")
local Build = game.ReplicatedStorage:WaitForChild("ChangeBuild")
local Visible = game.ReplicatedStorage:WaitForChild("Visible")
game.Players.PlayerAdded:Connect(function(plr)
	plr.Team = game.Teams.Lobby
end)

task.wait(5)
print("hello")
while true do
	--RoundModule.Notify("Not Enough Players! :(")
	--repeat wait() until RoundModule.Count() >= 2
	RoundModule.StartTimer(20)--Visual representation
	task.wait(20)--Functional Representation
	RoundModule.LoadMap()
	RoundModule.TeleportPlayers()
	RoundModule.Notify("Activate The Cube!")
	repeat wait() until RoundModule.CubeWasActivated
	Visible:FireAllClients(true)
	RoundModule.ChooseLeader()
	RoundModule.SendRaider()
	RoundModule.GrantTools()
	RoundModule.ConnectEvents()
	RoundModule.Waves = math.random(5,10)
	while RoundModule.CurrentWave < RoundModule.Waves and not RoundModule.UserLeft and not RoundModule.CubeDied do
		RoundModule.Notify("Build!")
		Build:FireAllClients(true)
		task.wait(1)
		RoundModule.StartTimer(60)
		task.wait(60)
		Build:FireAllClients(false)
		Raid:FireAllClients(true)
		RoundModule.Notify("Raid!")
		RoundModule.GrantWeapons()
		task.wait(1)
		RoundModule.StartTimer(20)
		task.wait(20)
		repeat wait() until RoundModule.EnemyCount() <= 0
		RoundModule.CurrentWave += 1
		RoundModule.Notify("Wave Ended!")
		task.wait(1)
		RoundModule.GrantResources()
	end
	RoundModule.Notify("Round Ended!")
	RoundModule.Disconnect()
	RoundModule.DestroyTools()
	RoundModule.TeleportToLobby()
	RoundModule.ResetTeams()
	RoundModule.DeloadMap()
	Visible:FireAllClients(false)
	task.wait(5)
end

here is the module itself:

local module = {}

module.Waves = 15
module.CurrentWave = 0
module.EnemyResourceIncrement = 250 * module.CurrentWave
module.PlayerResourceIncrement = 1000
module.CubeWasActivated = false
module.UserLeft = false
module.CanBuild = false
module.CanSpawn = false
module.Amountofplrs = 0
module.CubeDied = false

local Connections = {}

module.StartTimer = function(Time)
	local event = game.ReplicatedStorage.ChangeText
	while Time > 0 do
		event:FireAllClients("Time Left: "..Time)
		Time -= 1
		task.wait(1)
	end
end
module.TeleportPlayers = function ()
	local Players = game.Players:GetPlayers()
	for i,v in pairs(Players) do
		v.Character.PrimaryPart.Position = workspace:FindFirstChild("Map").SpawnBrick.Position + Vector3.new(0,2,0)
		module.Amountofplrs += 1
	end
	module.UserLeft = false
	module.CubeDied = false
end
module.LoadMap = function()
	local Map = game.ServerStorage:WaitForChild("Map"):Clone()
	Map.Parent = workspace
end
module.DeloadMap = function()
	workspace:FindFirstChild("Map"):Destroy()
end
module.TeleportToLobby = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		v.Character.PrimaryPart.Position = workspace.SpawnLocation.Position
	end
end
module.GrantResources = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams.Lobby then continue end
		if v.Team == game.Teams["The Raider"] then
			local raid = v.Backpack:FindFirstChild("RaidShop")
			if not raid then continue end
			raid:SetAttribute("Resources",raid:GetAttribute("Resources") + module.EnemyResourceIncrement)
		else
			local shoup = v.Backpack:FindFirstChild("Shop")
			if not shoup then continue end
			shoup:SetAttribute("Resources",shoup:GetAttribute("Resources") + module.PlayerResourceIncrement)
		end
	end
end
module.ChooseLeader = function()
	local Plrs = game.Players:GetPlayers()
	local Num = math.random(1,#Plrs)
	local Chosen = Plrs[Num]
	Chosen.Team = game.Teams["The Raider"]
	module.Amountofplrs -= 1
	for i,v in pairs(Plrs) do
		if v.Team == game.Teams["The Raider"] then continue end
		v.Team = game.Teams["The Defenders"]
	end
end
module.ConnectEvents = function()
	table.insert(Connections,game.Players.PlayerRemoving:Connect(function(plr)
		if plr.Team == game.Teams["The Raider"] then
			module.CurrentWave = module.Waves
			module.UserLeft = true
			module.Notify("Raider Left!")
		elseif plr.Team == game.Teams["The Defenders"] then
			module.Amountofplrs -= 1
		end
	end))
	for i,v in pairs(game.Players:GetPlayers()) do
		table.insert(Connections,v.CharacterRemoving:Connect(function(char)
			if v.Team == game.Teams["The Raider"] then
				task.wait(2)
				v.Character.PrimaryPart.Position = workspace:FindFirstChild("Map").RaiderSpawn.Position
			elseif v.Team == game.Teams["The Defenders"] then
				v.Team = game.Teams.Lobby
				module.Amountofplrs -= 1
			end
		end))
	end
end
module.GrantTools = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams["The Raider"] then
			local Clone = game.ServerStorage.Tools:FindFirstChild("RaidShop"):Clone()
			Clone.Parent = v.Backpack
		elseif v.Team == game.Teams["The Defenders"] then
			local clone = game.ServerStorage.Tools:FindFirstChild("Shop"):Clone()
			clone.Parent = v.Backpack
		end
	end
end
module.DestroyTools = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		local Tool = v.Backpack:FindFirstChild("RaidShop") or v.Backpack:FindFirstChild("Shop")
		if not Tool then continue end
		Tool:Destroy()
	end
end
module.Disconnect = function()
	for i,v in pairs(Connections) do
		v:Disconnect()
	end
end
module.ResetTeams = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		v.Team = game.Teams.Lobby
	end
end
module.Count = function()
	local counter = 0
	for i,v in pairs(game.Players:GetPlayers()) do
		counter += 1
	end
	return counter
end
module.Notify = function(Text)
	local event = game.ReplicatedStorage.ChangeText
	event:FireAllClients(Text)
end
module.EnemyCount = function()
	local Counter = 0
	for i,v in pairs(workspace.Enemy:GetChildren()) do
		Counter += 1
	end
	return Counter
end
module.SendRaider = function()
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team ~= game.Teams["The Raider"] then return end
		v.Character.PrimaryPart.Position = workspace:FindFirstChild("Map").RaiderSpawn.Position
	end
end
module.GrantWeapons = function()
	local Weapon = game.ServerStorage.Tools.CrossBow
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Team == game.Teams["The Raider"] then return end
		Weapon:Clone().Parent = v.Backpack
	end
end

return module

so i don’t know if you need this but:


and:
ReplicatedStorageyes
also i think it stops on the normal script at line task.wait but i’m not sure about that!

Thanks in advance!

Did you use ai to write this because this isnt lua code?

no, i wrote it myself, it is lua (i think, idk if it is luau or lua)

1 Like

Your timer not reaching 0 mostly has to do with the fact that you are sending the event before changing the time variable. Also I believe you will need to incorporate threading because, correct me if i am wrong but the code that goes after StartTimer in your main game loop does not run until the timer has ended?

module.StartTimer = function(Time)
	local event = game.ReplicatedStorage.ChangeText
	while Time > 0 do 
		
		Time -= 1
		event:FireAllClients("Time Left: "..Time)
		task.wait(1)
	end
end

edit: okay so i think i understand what you mean,
uhh so do i need to use wait instead of Task.wait because the event is firing not at the same time as task.wait?
(also what’s threading?)

task.wait and wait are the exact same thing. For multithreading in this case I would just use a coroutine.
I am no expert at threading but simply said it is a way of allowing 2 things to run at once.

module.StartTimer = function(Time)
	
	local event = game.ReplicatedStorage.ChangeText
	coroutine.wrap(function() -- Create new thread
		while Time > 0 do 
			Time -= 1
			event:FireAllClients("Time Left: "..Time)
			task.wait(1)
		end
	end)()
end
1 Like

omg it worked, so coroutine makes something that can handle two things at the same time? and the problem was that task.wait and old timer had a huge time difference? wow thank you!
edit: sentence

Like I said before the difference between task.wait() and wait() is practically 0. When you use task.wait() or wait() it puts the current thread you are using to sleep for the specified time. So when we create a new thread we now have 2 different timers running synchronously (at the same time).

1 Like

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