Why does my script count down so inaccuractly?

Hey there!

I am trying to make a value count down, but all it gives me in the output is this:

image

This is the code I am using to count down:

while true do
	wait(.1)
	if stunTime > 0 then
		stunTime = stunTime - .1
	end
end

Any help is greatly appreciated!

Use task.wait(TimeGoesHere) instead of wait(). This is because the regular wait() is throttled while task.wait() is based on the time that has past since the last heartbeat.

I still get the exact same output:
image

I don’t think it has to do with wait() being throttled.

I should clarify that I am printing stunTime

I don’t know why the numbers are doing that. If you want your output to be clean you can just round it:

math.round(stunTime * 10) / 10

This is good practice, but the actual reason for the bunch of decimals is float imprecision. Basically, floats only approximate real numbers and can’t store them 100% precisely. This means that your numbers can have those miniscule variations, which results in these ugly looking strings. You can get rid of them by presenting the number with a maximum number of digits after the decimal point and rounding the number based on it. This can be done with string.format(), using a specifier such as G, like this: string.format("%G", stunTime)

that seemed to work pretty well. Thanks!

Just as a side note, personally I like to just use regular integers for something like what you’re doing. Just factor everything up by a power of ten to whatever precision you need. Unless you’re doing heavier mathetical stuff where you actually want the float precision, floats mostly matter when you need to display a number to a player or perform a calculation with the number and in those instances you can just divide it by whatever you multiplied by. Of course thats not always the case, but it tends to be a lot quicker with less formatting in the end for me.

Case by case basis naturally.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.