How do I stop a timer if a BoolValue has been changed to be true?

The code below is what I use for the timer. How would I stop this if a BoolValue was set to true?

timeEvent = game.ReplicatedStorage.TimeEvent
		function Timer()

			local timeAmount = 180

			while timeAmount > 0 do

				timeEvent:FireAllClients(timeAmount)

				wait(1)

				timeAmount -= 1
				Check()
			end

		end
		Timer()
		

You can do this by simply adding a variable outside of the function and setting it to false. If you ever make it true, then the timer will stop but won’t reset (with this code at least).

timeEvent = game.ReplicatedStorage.TimeEvent
local stop = false

function Timer()

	local timeAmount = 180

	while timeAmount > 0 and not stop do

		timeEvent:FireAllClients(timeAmount)

		wait(1)

		timeAmount -= 1
		Check()
	end

end
Timer()

Also could I ask what the timeEvent:FireAllClients(timeAmount) does? I wouldn’t recommend firing a remote event every second.

What happens is if the value is set to true, a different result will happen than if it were set to false. The idea is you have a time limit to do a task, and if you run out of time, result A happens, and if you finish it before the time runs out, you get result B

FireAllClients(timeAmount) connects to a Gui that updates the timer.

local label = script.Parent



local ReplicatedStorage = game:GetService('ReplicatedStorage')

local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent')



timeEvent.OnClientEvent:Connect(function(timeAmount)

	label.Text = timeAmount

end)

I’d recommend putting another timer on the client so that it doesn’t have to fire a remote event every second. Its only purpose on the client would be to update the GUI, so you wouldn’t have to worry about the timer being exploited. Then, you can just fire a remote event when you want the timer to start or stop. This can help make your game possibly less laggier but also help with any players who are lagging.

Ok, I’ll see what I can do. Thanks!

1 Like