For loop being broken

For some strange reasons, the For loop doesn’t run to the end.
Example:

for i = 0, 1, 0.05 do
	frame.BackgroundTransparency = i
	wait(0.05)
end

The background transparency is supposed to end up at 1, but when I checked in the explorer, it showed 0.95. Is this a bug because when I tested the code with integers it worked fine?

The output when trying to print:
image

It should be fine, try placing the yield in front of the loop;

for i = 0, 1, 0.05 do
    wait(0.05)
	frame.BackgroundTransparency = i
end

Additionally, it seems as if you’re trying to tween background transparency straightforward, you may want to consider TweenService instead for this as it can create smoother transitions, animations and effects.

1 Like

Nope, still broken. I tried 0.5, it works but not 0.05
image

1 Like

maybe try making the end value 1.05?

it’s not supposed to end up at 1

That did fix the problem, but I want to know why it doesn’t end up at 1?

How exactly are you printing the values?

Just do a print(i) in the for loop.

for i = 0, 1, 0.05 do
	print(i)
    wait(0.1)
end

I believe this could be a floating point error that makes 0.05 added to 0.95 become greater than 1?

Source

In that case, test with a goal of 1.05, integers divided by integers, or a different loop that changes a created variable before the loop instead to see any difference.

Ideally, you should just use TweenService.

Thanks! Dividing the integers work. Interesting.