I'm trying to pause a round timer with a bool value

Hello, I’m trying to pause a for i loop (Round Timer), but the loop doesn’t work. Shouldn’t I use bool values with an if statement?

Here’s the timer module script:

local m = {}


local ServerStorage = game:GetService("ServerStorage")
local isTimerPaused = ServerStorage:WaitForChild("isTimerPaused")


local function toMS(s)
	return ("%002i:%02i"):format(s/60%60, s%60)
end


function m.StartTimer(Length)
		
	if isTimerPaused == false then
		
		for i=Length,0,-1 do
			
			game.ReplicatedStorage.Values.TimerValue.Value = toMS(i)
			
			wait(1)
			end
	else
		
		warn("This timer is Paused")
		
	end

end

game.ReplicatedStorage.Values.Status.Value = "Game Over!"
game.ReplicatedStorage.Remotes.GameOver:FireAllClients()

return m

Here’s the other script which the module script is inside:

local Timer = require(script.Timer)

game.ReplicatedStorage.Remotes.StartGame.OnServerEvent:Connect(function()
	Timer.StartTimer(50)
end)

Help would be appreciated.

You would have to put the bool check inside of the loop for it to actually pause.

Didn’t work

local m = {}
m.ClassName = "Timer"

local ServerStorage = game:GetService("ServerStorage")
local isTimerPaused = ServerStorage:WaitForChild("isTimerPaused")


local function toMS(s)
	return ("%002i:%02i"):format(s/60%60, s%60)
end

function m.StartTimer(Length)
		for i=Length,0,-1 do
			if isTimerPaused == false then
			game.ReplicatedStorage.Values.TimerValue.Value = toMS(i)
			
		wait(1)
		else
			warn("Paused")
		end
	end
end

game.ReplicatedStorage.Values.Status.Value = "Game Over!"
game.ReplicatedStorage.Remotes.GameOver:FireAllClients()

return m

It just keeps pausing when it’s false too.

You don’t want an else statement as the loop is going to run regardless. You’ll want an if statement with an indefinite yield.

for i=length,0,-1 do
	task.wait(1)
	--code for timer -1
	while paused do task.wait() end
end

However, it would be more efficient to use something like a BoolValue or Attribute and use the changed event yield.

Doesn’t pause with minutes to seconds,
and using only for i=Length into TimerValue (I don’t know how to explain the only seconds part)

for i=Length,0,-1 do
		task.wait(1)
		game.ReplicatedStorage.Values.TimerValue.Value = Length
		while isTimerPaused == false do task.wait() end
	end

Pauses even when the bool is false?
But I’m making only minutes to seconds timer?
Am I doing something wrong again?

Sorry, I’m not really sure what you’re trying to say.

Also the statement should be:

while isTimerPaused do task.wait() end

Lowered quality for file size.

Is there some kind of Remote? Because that BoolValue is in ServerStorage and only visible to the server.

There’s a remote to start the round (timer and other), and a game over when the timer runs out. I’m gonna try putting in ReplicatedStorage if it works.

A remote would have to tell the client when the timer is paused or unpaused, otherwise you’ll have to move it to ReplicatedStorage.


:thinking:

Ah, I see the issue. Just a small little mistake:

while isTimerPaused.Value do task.wait() end
1 Like

Works, THANK YOU SO MUCH!!!

1 Like