How do you create a timer that counts a players time?

put this under a local script inside of a textlabel:

local milisecond = 0
local second = 0
local minute = 0
local hour = 0
while wait(0.01) do
	if milisecond >= 60 then
		milisecond -= 60
		second += 1
	end
	if second >= 60 then
		second -= 60
		minute += 1
	end
	if minute >= 60 then
		minute -= 60
		hour += 1
	end
	milisecond += 1
	script.Parent.Text = hour..":"..minute..":"..second.."."..milisecond
end
5 Likes

Yeah it would take me a lot more time to make than this dude

thanks for the compliment :smile:

I would recommend against this method, as using wait(0.01) could cause problems. wait() is known to have problems with it not correctly waiting for the small amount of time like 0.01. I suggest you re-implement this solution with something like tick().

4 Likes

U probably type like a godzilla lol

well this doesnt have any problems at all, I just tested it. it doesnt take up much memory and doesnt cause lag. It’s a fully functioning timer.

Well how many players did you have in your testing? How many things were going on? I’m saying that I myself have had problems using wait(), and I am simply suggesting that tick() is a safer solution that can have more decimal places if ever necessary in the future, and it’s usage is actually meant to count time in seconds.

2 Likes

Then we could also use os.time if it comes to secs

even so, its local so it wouldnt cause any lag as it doesnt run other peoples timer. It does not have any problems at all, I get that in different ways tick() can be better but for this its perfectly fine and as for “wait having problems with small numbers” it with something like this. It only has problems with small numbers if you’re having a lot of things running from it within that short time.

There are a lot of ways you can do that, an option would be using os.time

Or creating a timer by yourself.

Yes that is another option as well.

The point is, doing wait() manually worse than using tick(), os.time(), or another similar method that’s meant to be used for that, as well as accurately counts the time - free of lag or most problems you could encounter with wait().

1 Like

Also, it’s not actually waiting for 0.01 seconds to let you know. The fastest you can do with wait() is 0.03, no lower. So it’s not working correctly. I recommend you switch it to an os.time or tick() method instead.

1 Like

As I stated, it runs perfectly fine on a 0.01 time. I’m not trying to fight.

So, if I were to add to add an if statement to the script it doesn’t work. I stated if player.Started.Value == true then the timer would start but it didn’t.

No it does not. You’re not aware that it’s only running at 0.03 seconds, not 0.01? The limitation of wait() is 0.03, which is about 1/30th of a second.

Take a look for yourself: Decrease the minimum time in wait() to 1/60th of a second

And sorry, I don’t want to be rude at all. I just want to help out to make the solution work.

2 Likes

well, I havent really done time based scripts so I am not used to using tick() or os.time much. So I wouldn’t be able to make it different anyways.

here is a script that will run in the way you wanted it to. It will only give the time if they have started.

local milisecond = 0
local second = 0
local minute = 0
local hour = 0
while wait(0.01) do
	if milisecond >= 60 then
		milisecond -= 60
		second += 1
	end
	if second >= 60 then
		second -= 60
		minute += 1
	end
	if minute >= 60 then
		minute -= 60
		hour += 1
	end
	if game.Players.LocalPlayer.Started.Value == true then
		milisecond += 1
	end
	script.Parent.Text = hour..":"..minute..":"..second.."."..milisecond
end
1 Like

So even though you are defining wait(0.01) its not gonna work at all because the wait() can only go to 0.03 so your script is not gonna work as intended because its gonna run .02 seconds slower than it is meant to. The script may still work because Lua is a forgiving language but your time will always be off. I believe if you change wait to tick() it should fix that problem because the tick() function allows you to start from 0.01.

1 Like

Sorry! Another question. How would I stop the timer and reset it if a player dies or touches the end part?

it already automatically resets if they die. now if you wanted to reset it, just destroy the gui and replace it.