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

Timer counts up to negative numbers after counting down?

I'm making a script giving how many seconds until the next round. The problem is that the GUI seems to count up to negative numbers (such as -1, -2, etc.) instead of fully stopping at 0.

local time = 20
for i = 1,20 do
	wait(1)
time = time - 1
	hintText.Text = "Next round in: "..tostring(time)
end

1 comment

0

This is in a LocalScript.

2 answers

✓ Accepted answer

Instead of taking way one from a time variable, I would suggest modifying your for loop so that it counts backwards from 20 to 1. To count backwards in a for loop, there's a third argument you can give which is the increment.

With this in mind, we can make the increment -1 and it will count backwards from 20 to 1 and then set i in the hint text, instead of the time variable that we don't need.


Final code:

--No need for the time variable
for i = 20,1,-1 do --For loop, wth the variable i starting at 20 and ending at one with an increment of -1.
	hintText.Text = "Next round in: "..i --No need to convert to a string here, numbers will be converted to strings automatically when concatenated.
	wait(1) --I like to wait after setting the text so that the text is instantly set in the for loop first.
end

I hope my answer helped you. If it did, be sure to accept it.

Why dont you use your for loop instead.

for i =1,20,1 do

hintText.Text = " Text here "..i

end

2 comments

0

Sorry im on the phone

0

He wants the time to go down, so you need to have 20 as the first argument, 1 as the second argument and -1 as the third, optional argument.

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