My script breaks when it reaches 0

I found what a think is a glitch, when I subtract from a number and try to get to it returns a very odd number. my script:

local TransCount = 1

	while true do 
		NewScythe.Handle.Transparency = TransCount
		NewScythe.Handle.Union.Transparency = TransCount
		TransCount = TransCount - 0.1
		wait()
		print(TransCount)
		if TransCount == 0 then
			break
		end
	end

what it returnsScreen Shot 2021-10-16 at 4.26.34 PM
I do not have any clue as to why it does this, anyone know?

Try replacing == with <=, so

local TransCount = 1

	while true do 
		NewScythe.Handle.Transparency = TransCount
		NewScythe.Handle.Union.Transparency = TransCount
		TransCount = TransCount - 0.1
		wait()
		print(TransCount)
		if TransCount <= 0 then
			break
		end
	end

thanks it works! but does anyone know why I get that odd number?

Just to explain it, the weird number is actually a number super close to 0. This issue is something very common when dealing with floats/doubles (number with a decimal in it.) Its called a floating point error.

You can check if the number is close to 0, or use normal integer (whole numbers) instead and divide it by 10 before you use it.

2 Likes

ok, thank you! I have never heard of this type of floating point error before, but I have heard of floating point errors.

Check this stack overflow answer where he explains it in more detail:

1 Like

Cleaned it up a bit too.

local TransCount = 1

repeat wait()
	NewScythe.Handle.Transparency = TransCount
	NewScythe.Handle.Union.Transparency = TransCount
	TransCount -= 0.1
	print(TransCount - TransCount % (0.1 or 1))
until TransCount <= 0.1