How do I pause this countdown?

I want to pause this countdown once the flow is no longer happening, this is my script but it still keeps counting down

airlabel = script.Parent.AirLabel
on = script.Parent.AirLabel.On
off = script.Parent.AirLabel.Off
flow = false

on.MouseButton1Click:Connect(function()
	if flow == false then
		for i = 100, 0, -3 do
			airlabel.Text = i
			wait(1)
			flow = true
			if flow == false then
				repeat wait() until flow == true
			end
		end
	end
end)

off.MouseButton1Click:Connect(function()
	if flow == true then
		flow = false
	end
end)
1 Like

The if statement already ran. Put it in the for loop. Also you can use a break statement to just break the loop.

1 Like

I don’t get this code totally. Can you explain a little bit more about what would you want this code to do?

To pause the countdown, you can add a repeat...until loop in your on.MouseButton1Click function that waits until flow is set to true. For example

airlabel = script.Parent.AirLabel
on = script.Parent.AirLabel.On
off = script.Parent.AirLabel.Off
flow = false

on.MouseButton1Click:Connect(function()
	if flow == false then
		for i = 100, 0, -3 do
			airlabel.Text = i
			wait(1)
			flow = true
			if flow == false then
				repeat wait() until flow == true
			end
		end
	end
end)

off.MouseButton1Click:Connect(function()
	if flow == true then
		flow = false
	end
end)

This will pause the countdown when the off button is clicked and resume it when the on button is clicked again. Your code is kinda messed up

1 Like

Kind of an understatement. There’s quite a bit of bad practices.

Can I get some tips on my bad practices

How do I keep it from repeating? When it hits 0 it goes back to 100 but I tried breaking it

Use task.wait (just write task. Before wait)
Don’t use repeat until value. Add the value in workspace and use .Changed. it does the same thing except it’s using less resources.
Also, if you need the player’s character (pretty unrelated), instead of using repeat do:

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait() --basically, it assigns the character, but if it doesn't exist it'd error, but it sees the or. :Wait is basically connect except for the fact it waits until the event fires, and then it continues. No functions and only fires once since it just yields.

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