Make a countdown continue after pausing

I am making a system where you drill through a safe. Currently, the drill has a small chance to break. If the chance succeeds, the player must fix the drill.
The problem is that even after the drill is fixed, it does not continue the countdown.

Code:

function Countdown(Time, Display)
	local CurrTime = Time
	local Drilling = true
	local DoneDrilling = false
	local TimesBroken = 0
	local BreakChance = CurrSessionInfo.DrillBreakChance
	local MaxBreaks = CurrSessionInfo.MaxDrillBreaks
	while Drilling do
		CurrTime = CurrTime - 1
		Display.Text = (CurrTime .. " SECONDS")
		if CurrTime <= 0 then
			print("Drill finished!")
			DoneDrilling = true
			Drilling = false
			return true
		else
			local RandVal = math.random(1, 100)
			if RandVal <= BreakChance.Value and TimesBroken < MaxBreaks.Value then
				print("Drill broken!")
				TimesBroken = TimesBroken + 1
				Drilling = false
				local Prompt = Instance.new("ProximityPrompt")
				Prompt.Parent = Display.Parent.Parent.Parent.Parent.PrimaryPart
				Prompt.RequiresLineOfSight = false
				Prompt.MaxActivationDistance = 14
				Prompt.ActionText = ("Restart")
				Prompt.ObjectText = ("Drill")
				Prompt.HoldDuration = 4
				local Sound = Display.Parent.Parent.Parent.Broken
				Sound:Play()
				Prompt.Triggered:Connect(function()
					print("Drill fixed!")
					Prompt:Destroy()
					Sound:Stop()
					Drilling = true
				end)
			end
		end
	task.wait(1)
	end
end

Any help would be much appreciated!

1 Like

try using a repeat loop until the drilling is set back to true after the prompt.Triggered, like this:

Prompt.Triggered:Connect(function()
	print("Drill fixed!")
	Prompt:Destroy()
	Sound:Stop()
	Drilling = true
end)
repeat task.wait() until Drilling
1 Like

I don’t quite understand how to implement this. This is the modified code I tried to use, and it’s not quite working:

function Countdown(Time, Display)
	local CurrTime = Time
	local Drilling = true
	local DoneDrilling = false
	local TimesBroken = 0
	local BreakChance = CurrSessionInfo.DrillBreakChance
	local MaxBreaks = CurrSessionInfo.MaxDrillBreaks
	while true do
		local RandVal = math.random(1, 100)
		if RandVal <= BreakChance.Value and TimesBroken < MaxBreaks.Value then
			print("Drill broken!")
			TimesBroken = TimesBroken + 1
			Drilling = false
			local Prompt = Instance.new("ProximityPrompt")
			Prompt.Parent = Display.Parent.Parent.Parent.Parent.PrimaryPart
			Prompt.RequiresLineOfSight = false
			Prompt.MaxActivationDistance = 14
			Prompt.ActionText = ("Restart")
			Prompt.ObjectText = ("Drill")
			Prompt.HoldDuration = 4
			local Sound = Display.Parent.Parent.Parent.Broken
			Sound:Play()
			Prompt.Triggered:Connect(function()
				print("Drill fixed!")
				Prompt:Destroy()
				Sound:Stop()
				Drilling = true
			end)
			repeat task.wait(1) until Drilling
				while Drilling do
				CurrTime = CurrTime - 1
				Display.Text = (CurrTime .. " SECONDS")
				if CurrTime <= 0 then
					print("Drill finished!")
					DoneDrilling = true
					Drilling = false
					return true
				end
				task.wait(1)
			end
		end
	end
	task.wait(1)
end

It always instantly breaks the drill upon being placed, and then never breaks the drill again.

Make a variable for the remaining time and break out of the loop. Once you want to start it back up again, just take that variable and make a new for loop with it.

1 Like

I tried this, but now it resets the timer every time the drill is restarted.

function Countdown(Time, Display)
	local CurrTime = Time
	local Drilling = true
	local DoneDrilling = false
	local TimesBroken = 0
	local BreakChance = CurrSessionInfo.DrillBreakChance
	local MaxBreaks = CurrSessionInfo.MaxDrillBreaks
	while Drilling do
		CurrTime = CurrTime - 1
		Display.Text = (CurrTime .. " SECONDS")
		if CurrTime <= 0 then
			print("Drill finished!")
			DoneDrilling = true
			Drilling = false
			return true
		else
			local RandVal = math.random(1, 100)
			if RandVal <= BreakChance.Value and TimesBroken < MaxBreaks.Value then
				print("Drill broken!")
				TimesBroken = TimesBroken + 1
				Drilling = false
				Break(Time, Display)
			end
		end
	task.wait(1)
	end
end

function Break(Time, Display)
	local Prompt = Instance.new("ProximityPrompt")
	Prompt.Parent = Display.Parent.Parent.Parent.Parent.PrimaryPart
	Prompt.RequiresLineOfSight = false
	Prompt.MaxActivationDistance = 14
	Prompt.ActionText = ("Restart")
	Prompt.ObjectText = ("Drill")
	Prompt.HoldDuration = 4
	local Sound = Display.Parent.Parent.Parent.Broken
	Sound:Play()
	Prompt.Triggered:Connect(function()
		print("Drill fixed!")
		Prompt:Destroy()
		Sound:Stop()
		Countdown(Time, Display)
	end)
end
1 Like

just put a repeat task.wait() until Drilling after the triggered event, nothing else

1 Like

Make it a public variable, instead of it inside a function

1 Like

Multiple drills can exist in the workspace at once, this would make them no longer work as intended.

1 Like

Better yet, instead of polling just yield the thread until the prompt is triggered.

Prompt.Triggered:Wait()
2 Likes