Doing a timer but

Why 4.5399999999 appears from 4.52 seconds

for i = 0, 50, 0.01 do

	game.Players.LocalPlayer.Timer.Value = i
	wait()
end

That is because of the glorious phenomenon known as floating point value discrepancies. I haven’t read too much on why this is so all I can tell you is don’t worry about it.

what happens that I’m doing a timer and when “4.5399999” appears in the TextLabel it appears like this and I just want it to appear 4.53 and not the 999999

Try using:

tostring(math.floor(I))

instead of just i.

EDIT: tostring() converts it into a string so don’t use that if it’s a number value.

yes but that transforms “i” into 4

Ah, I see. For this, you’ll have to use a custom rounding function OR you can use a string editing function that removes any strings after 4 characters in a string. (try string.sub())

1 Like

If you want to make it accurate to 2 decimal places you can do something like this, although this is untested since I’m not on Studio right now:

game.Players.LocalPlayer.Timer.Value = math.floor(i * 100) / 100

On second thought, do you have any reason to not put the timer into a variable? Repeatedly indexing the object hurts performance. To put the timer into a variable, the code would change into something like this:

local timer = game.Players.LocalPlayer.Timer
for i = 0, 50, 0.01 do
	timer.Value = math.floor(i * 100) / 100
	wait()
end

I also recommend using game:GetService() to access any service rather than typing in game.Service. For the Players service this would become:
game:GetService("Players")

3 Likes