Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my For Loop end at "1.3877787807814e-16" rather than "0"?

I've made 2 LocalScripts that serve as Fade In and Fade Out effects for my ScreenGui:

--Fade Out

local Screen = script.Parent
		
Screen.BackgroundTransparency = 0
Screen.Visible = true
		
for Trnsprncy = 0, 1, 0.1 do
	wait(0.01)
	print(Trnsprncy)
	Screen.BackgroundTransparency = Trnsprncy
end
		
Screen.Visible = false

--Fade In

local Screen = script.Parent
		
Screen.BackgroundTransparency = 1
Screen.Visible = true

for Trnsprncy = 1, 0, -0.1 do
	wait(0.01)
	print(Trnsprncy)
	Screen.BackgroundTransparency = Trnsprncy
end

The Fade Out script works as intended, simple and smooth, however the Fade In script decreases as follows (Printed in the Output):

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.3877787807814e-16

Because of the exaggerated number at the end, the Frame doesn't go completely black and instead displays as if the BackgroundTransparency was "1"

I just want to know why does this happen specifically when decreasing the variable and how can I fix the For Loop.

1 answer

✓ Accepted answer

Do this, I do this instead of loops (Tweening)

local transparencyGoal = {BackgroundTransparency = 1}
local transparencyInfo = TweenInfo.new(1, Enum.EasingStyle.Linear) 
local TransparencyTween = TweenService:Create(Screen, transparencyInfo, transparencyGoal) 

-- Makes the backgroundTransparency transparent in 1 second
TransparencyTween:Play() 

If you wanted the loop script then

--Fade Out

local Screen = script.Parent

Screen.BackgroundTransparency = 0
Screen.Visible = true

for Trnsprncy = 0, 1, 0.1 do
    wait(0.01)
    print(Trnsprncy)
    Screen.BackgroundTransparency = Trnsprncy
end

Screen.Visible = false

--Fade In

local Screen = script.Parent

Screen.BackgroundTransparency = 1
Screen.Visible = true

for Trnsprncy = 1, 0, -0.1 do
wait(0.001) 

-- added if statements, indentions off. 
if Trnsprncy >= 0 and not Trnsprncy <= 0 then
    
    print(Trnsprncy)
    Screen.BackgroundTransparency = Trnsprncy

end
end

I think there might be a problem on the loop script, but let me know if something goes south

1 comment

0

The Tweening method works flawlessly, however the For Loop method errors at line 27: "Attempt to compare number with boolean". I'd consider this question solved so thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.