Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

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! Mordevifer 106 — 3y
Ad

Answer this question