How to stop transparency from going into the negatives

Hello,

I’m trying to fade a frame in and then out after the transparency reaches 0. I’ve got the fading in part down, but the transparency does not stop and increase when it hits 0, but instead starts going into the negatives.

How do I fix this and add transparency when the frame hits 0 transparency?

Script:

    local MainFrame = script.Parent
    local LoadingGui = script.Parent.Parent

        while wait(0.01) do
        	if MainFrame.BackgroundTransparency <= 1 then
        		MainFrame.BackgroundTransparency -= 0.01
        	if MainFrame.BackgroundTransparency == 0 then do
        				MainFrame.BackgroundTransparency += 0.01
        			end
        		end
        	end
1 Like

after it gets to zero write

break
1 Like

Like this? 30000000000000000000000000

no

local MainFrame = script.Parent
    local LoadingGui = script.Parent.Parent

        while wait(0.01) do
        	if MainFrame.BackgroundTransparency <= 1 then
        		MainFrame.BackgroundTransparency -= 0.01

        	if MainFrame.BackgroundTransparency == 0 then do

        				MainFrame.BackgroundTransparency += 0.01
break until 
MainFrame.BackgroundTransparency <= 1
MainFrame.BackgroundTransparency -= 0.01

        			end
        		end
        	end

While someone has suggested a solution, I highly recommend you use TweenService. It cleans up your code quite a bit, and it’s way easier to manage.

local tweenService = game:GetService('TweenService')

local Tween = tweenService:Create(MainFrame, TweenInfo.new(1) --[[time your tween takes]], {BackgroundTransparency = 1})
Tween:Play()

Check out the Developer article here:

3 Likes

says it ever line I put it at

Or the OP could use a for loop, it’ll be able to reduce the transparency rather than using a whole loop.

1 Like

True, thing is that tweens are super smooth assuming they’re being handled on the client, loops can be a bit choppy.

1 Like

Very true. That’s a better solution due to performance and memory.

1 Like

when you make a loop and u want it to stop at a certain statement of ur script, u just use the break keyword

if MainFrame.BackgroundTransparency == 0 then
    break
end

You can either use TweenService or use the math.clamp Roblox provides:

MainFrame.BackgroundTransparency = math.clamp(MainFrame.BackgroundTransparency + 0.01, MinimumNumber, MaximumNumber)

Basically, the number cannot go under the specified minimum value and over the specified highest value.

How would I make it fade in AND out over a certain period of time?

1 Like
TweenInfo.new(1 --[[specific time it takes to tween]], Enum.EasingStyle.Sine,  Enum.EasingDirection.InOut, 123 --[[the amount of times you want it to repeat]], true --[[whether it reverses or not, set to true for what you want to do.]])

Should I use while wait or while true do?

1 Like

You don’t have to use any. It’s built into tweenservice.

Where does it change the background transparency? Could you type the whole local tween = thing out please?

1 Like
local Tween = tweenService:Create(MainFrame, TweenInfo.new(1 --[[specific time it takes to tween]], Enum.EasingStyle.Sine,  Enum.EasingDirection.InOut, 123 --[[the amount of times you want it to repeat]], true --[[whether it reverses or not, set to true for what you want to do.]])) --[[time your tween takes]], {BackgroundTransparency = 1})
Tween:Play()

You don’t need any of this, just replace it with this:

local tweenService = game:GetService('TweenService')

local Tween = tweenService:Create(MainFrame, TweenInfo.new(1 --[[specific time it takes to tween]], Enum.EasingStyle.Sine,  Enum.EasingDirection.InOut, 123 --[[the amount of times you want it to repeat]], true --[[whether it reverses or not, set to true for what you want to do.]])) --[[time your tween takes]], {BackgroundTransparency = 1})
Tween:Play()

For this use math.abs which will return the absolute value of the transparency. Which will prevent it from going into the minus.

1 Like

I cannot thank you enough, it sounds like im a noob but I can script a lot more advanced things. I’ve been caught on this for 5 hours. TY

1 Like