Countdown Not Working Properly

Hello. I am trying to make a countdown where it counts down 60 seconds before saying one out of 2 random answers, but my countdown is giving me a little trouble. I say to go down by 1 every second but when I play it immediately jumps down to 0. I have tried to change the scale but it gives me weird numbers. Please help.

local check = true
while check == true do
		check = false
		for countdown = 60 , 0, -1 do
			game.Workspace.Timer.SurfaceGui.TextLabel.Text = countdown
			if countdown == 0 then
				math.random(1, 2)
				if math.random == 1 then
					game.Workspace.Timer.SurfaceGui.TextLabel.Text = "FLOOD!"
				else
					game.Workspace.Timer.SurfaceGui.TextLabel.Text = "LAWNMOWER!"
			end
		end
	end
end

If you can, I would also like to make sure that I am using the math.random right, where I want either a 1 or a 2 and want those stuff to happen. Thanks!

You need to add a wait in the loop so that it waits a second before checking again

1 Like

Ok thanks. Didn’t know I had to do that because it worked fine without it in developer.

Just add yield the thread for 1 second:

local check = true
while check == true do
		check = false
		for countdown = 60 , 0, -1 do
			game.Workspace.Timer.SurfaceGui.TextLabel.Text = countdown
			if countdown == 0 then
				math.random(1, 2)
				if math.random == 1 then
					game.Workspace.Timer.SurfaceGui.TextLabel.Text = "FLOOD!"
				else
					game.Workspace.Timer.SurfaceGui.TextLabel.Text = "LAWNMOWER!"
			end
		end
        wait(1)
	end
end
1 Like

Your code is running almost immediately, be sure you are putting some form of wait in to count down per second. So that it waits one second before running whatever the rest of it is, or rerun through the loop.

3 Likes

math.random is a function that returns a random number so you would just store the output in a variable
like this
local number = math.random(1,2)
if number == 1 then
–RunCode

1 Like

Adding onto what is already said, I recommend you use os.clock() for counting time. It is more reliable. In your case, a laggy server would make the timer run slower than wanted. Look up some good examples on hoe to use os.clock().