How do I stop this timer from duplicating?

So I have this timer script:

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local gameOver = false



local seconds = info.Time.Seconds
local function timer()
	seconds.Value = 0
	repeat
		seconds.Value += 1
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value
end

info.Wave.Changed:Connect(function()
	timer()
end)

However, after each wave I think its duplicating itself because it then goes from 1 count every second to 2 and then 3, etc. How can I fix this?

3 Likes

maybe use a debounce if u can?
its like

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local gameOver = false
local debounce = false


local seconds = info.Time.Seconds
local function timer()
	seconds.Value = 0
	repeat
		seconds.Value += 1
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value
	debounce = false
end

info.Wave.Changed:Connect(function()
	if not debounce then
		debounce = true
		timer()
	end
end)
1 Like

But that would make it so it only happens once. I want it to keep counting and restart after every wave.

2 Likes
...
local db = false
local function timer()
      if db == true then return end 
       db = true -- stop any code to run the code while the code running
       seconds.Value = 0 
       while  #workspace.Mobs:GetChildren() >0 do -- check if the mobs greater then 0
	      seconds.Value += 1
           task.wait(1)
       end -- stuck
      db = false -- change it so it can run again
end
...
1 Like

The issue is your function can run again and again even if there is already another function is running.

1 Like

oh sorry I made a mistake I will edit it to the correct code

How do I stop that then? Like stop multiple running at once

Try this

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local gameOver = false



local seconds = info.Time.Seconds
local db = false
local function timer()
      if db == true then return end 
       db = true -- stop any code to run the code while the code running
       seconds.Value = 0 
       while  #workspace.Mobs:GetChildren() >0 do -- check if the mobs greater then 0
	      seconds.Value += 1
           task.wait(1)
       end -- stuck
      db = false -- change it so it can run again
end


info.Wave.Changed:Connect(function()
	timer()
end)
1 Like

That didn’t seem to work, it just didnt count at all.

this is the corrected version of my script

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local gameOver = false
local debounce = false


local seconds = info.Time.Seconds
local function timer()
	seconds.Value = 0
	repeat
		seconds.Value += 1
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value
	debounce = false
end

info.Wave.Changed:Connect(function()
	if not debounce then
		debounce = true
		timer()
	end
end)
2 Likes

It works well but when you skip the wave and not all the enemies are killed, it keeps counting and doesn’t restart to zero. I tried doing this:

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local debounce = false


local seconds = info.Time.Seconds
local currentWave

local function timer()
	seconds.Value = 0
	currentWave = info.Wave.Value
	repeat
		seconds.Value += 1
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value or currentWave ~= info.Wave.Value
	debounce = false
end

info.Wave.Changed:Connect(function()
	if not debounce then
		debounce = true
		timer()
	end
end)

But the timer just froze at the new wave, what else can I do?

1 Like

By the way the issue is that when the wave is skipped, it doesn’t restart back to zero due to it not meeting the requirements. For some reason, the script above didnt work.

maybe try this

local Players = game:GetService("Players")
local ReplicatedStroge = game:GetService("ReplicatedStorage")
local functions = ReplicatedStroge:WaitForChild("Functions")
local wave = workspace.Info.Wave
local info = workspace.Info
local debounce = false
local seconds = info.Time.Seconds
local currentWave

local function timer()
	seconds.Value = 0
	currentWave = info.Wave.Value
	repeat
		seconds.Value += 1
		task.wait(1)
	until #workspace.Mobs:GetChildren() == 0 or not info.GameRunning.Value or seconds.Value >= (seconds u want for the wave to end)
	debounce = false
end

info.Wave.Changed:Connect(function()
	if not debounce then
		debounce = true
		timer()
	end
end)

Hey, thanks for all the help but I was able to solve it. I remembered I had a value called “Skip” for skipping waves obviously which I added to the list of conditions and it worked fine. Thanks anyways though!

1 Like

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