Roblox having trouble with simple counting

I’m trying to make a script that displays the timer for reloading a gun onto a gui. To do this, I simply have a variable “reloadcount” that counts down until reloading should be done. It starts at 2.5 and goes down in increments of 0.1. The loop ends when the number hits 0.

			repeat 
				if reloading == true then
					print(reloading)
					AmmonGUI.Text = "Reloading "..reloadcount
					reloadcount = reloadcount - 0.1
					print(reloadcount)
					if reloadcount == 0 then
						reloading = false
					end
					wait(0.1)
				end
			until reloading == false

(using until reloadcount == 0 causes errors elsewhere)
The loop for some reason doesnt end. At first I thought it was an error of the number simply not counting down, so I added a print(reloadcount) to see if reloadcount was counting down. To my surprise, Roblox doesnt know how to count. It counts down fine until it reaches 0.2. After that, it goes to 0.099999999999999 and then to -1.0824674490095e-15 and then to -0.1. Any help?

That’s odd, i don’t really have any fix for that but what you could do for now is put “if reloadcount <= 0 then” until you find one? also might be worth checking out the reloadcount variable itself

1 Like

Use

until reloadcount <= 0

This is most likely just floating point precision errors. Either way, never compare for exact numbers. Give a little headway.

4 Likes

I would say it’s because the wait(0.1) won’t wait exactly 0.1. If the amount of load on the computing system is a lot, then the wait(0.1) will actually last longer than 0.1. I agree with the others, do a less than or equal to operator to provide headway. I am pretty sure Roblox knows how to count lol. Very sensationalist title/discovery you made

the problem with that is that it looks bad in the GUI

These solutions could help with making it look better in the GUI:

You can use them to round your decimal to the nearest 0.1.

2 Likes