How to make a second and milisecond countdown

Code:


local seconds = 10

local miliseconds = 99

while wait(0.001) do

      if miliseconds > 00 then

              miliseconds -= 1

             else

                    seconds -= 1

       end

       script.Parent.Text = seconds..”.”..miliseconds

end

My desired result: PET SIMULATOR X TRADING MONTAGE #13 - YouTube

Timestamp: 0:21

Sadly wait()cant delay any lower than 0.03 and task.wait() waits 0.016 , so your best choice is task.wait() and adding .016, seconds, wont be a big difference.

1 Like

You could use tick() or task.wait() in that case because wait() has a delay

local Start = tick()

while task.wait() do
    local Diff = tick() - Start
    local Min = math.floor(Diff / 60)
    local Sec = Diff - Min * 60
    local Mil = math.floor((Sec - math.floor(Sec)) * 10)
    Sec = math.floor(Sec)
    script.Parent.Text = tostring(Sec) .. "." .. tostring(Mil)
end

Just noticed that this is actually counting up :sweat_smile:

3 Likes

so then how did the psx dev do it?

psx? What is that? Sorry if im dumb ;-;

short for pet simulator x

(extra owdoawhdoahdo)

Ohhhh right, i haven’t played it in so long, but i told you that task.wait gives you nearly a millisecond

Using tick(), which was suggested in a reply above the message you sent

Maybe try using os.difftime.

@FourFizzy and @good1005 but that script still waits while task.wait() do, so it steps every .016 seconds, not in milliseconds.

I think task.wait() would work just fine if they are going for the same result of what the video had ( so for example it would say “3.5 seconds left” )

Would this be actually a millisecond or close to it

The short answer is that it’s not possible to get a millisecond countdown. The shortest wait is task.wait which, as others have stated, has a delay of about 0.016 or 16 milliseconds. What you can do is this:

local seconds = 10

while seconds >= 0 do
	script.Parent.Text = math.round(seconds * 10) / 10
	seconds -= task.wait()
end

EXPLAINATION
The seconds holds the count total wait time. task.wait() returns the actual time it waited, so that value is subtracted from seconds each time the loop iterates. I put the text before the wait because the loop exit condition is that seconds is >= 0. When seconds goes negative, the loop exits. The text line multiplies the seconds by 10, rounds the result, then divides it by 10 to get a 0.1 second countdown. You don’t want to go much more than that because of network latency.

1 Like

Very close to it, and the closest you can get just like @msix29 and @Maelstorm_1973 (also many others) just said

1 Like

How do I set my own amount of seconds and how do I stop the counter

for Index = 10, 0, -0.1 do
    script.Parent.Text = '⏳ '..Index

    if Index == 0 then
        script.Parent.Text = '✅ Complete!'
        break
    end

    task.wait(0.1)
end

This would be the most condensed code for this you’ll probably get. (Don’t reply just to challenge that statement, I’m not looking for that.)

There is no delay so it will finish in less than a millisecond/instantly.

1 Like

I just realized that, and I fixed it.

1 Like

task.wait() cant go under 0.16 seconds, i did mention it before, there is no difference between what you did and what everybody said.

You should generally avoid while loops as much as possible, unless necessary. This would be a better practice as it avoids using spawn function(s), to prevent unnecessary threads that can be avoided, (utilizing my code above).

Now hey, you can do as you please with while loops and I’m not one to judge that, however, this is a better alternative to a while loop to use as a countdown, especially in this situation.

We are taking about the delay and not how we implement it, so no need to care about this one now.