Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I stop the wait function?

How would I make it so when the value is set to Reset, it stops the wait function in the if statement above it 'wait(roundtime)'.

local rs = game:GetService('ReplicatedStorage')
local timerv = rs:WaitForChild("Timer")
local roundtime = 60

timerv.Changed:Connect(function()
	if timerv.Value == "Run" then
		wait(roundtime)
		timerv.Value = "Done"
	elseif timerv.Value == "Reset" then
		print("Reset")
			
	end
end)

1 comment

0

you can't stop a wait function once it is ran

2 answers

Insert at line 4

reset = false

Replace line 7 with

local countDown = roundtime
while not reset and countDown > 0 do
wait(1)
countDown = countDown - 1
end
reset = false

Insert on line 11

reset = true

1 comment

0

Didn't work. It still destroyed the item after 20 seconds instead of 60. Basically, after I guess the answer multiple times correctly and just let it wait, it only waits 20 seconds instead of 60.

You can use a for loop and 'break out' of the loop.

local rs = game:GetService('ReplicatedStorage')
local timerv = rs:WaitForChild("Timer")
local roundtime = 60

timerv.Changed:Connect(function()
	if timerv.Value == 'Run' then 
		for i = roundtime, 0, -1 do
			if timerv.Value == 'Reset' then 
				break
			end
			print(string.format('Game time: %s', i))
		end
		timerv.Value = timerv.Value == 'Run' and 'Done' or 'Reset'
		-- if timer.Value still equals Run, then it ran without a Reset
		-- else, game got reset
	end
end)

2 comments

0

Didn't work.

0

Read your output, figure out why. Don't stop at "It doesn't work".

This discussion is preserved as it was. New answers, comments, and votes are closed.