How do I make a Frame fade in

I’m trying to make a script that makes its parent Frame fade in/fade out when I press the insert key. The problem is that I can only make it fade out but I don’t know how to make it fade back in. I have looked everywhere and I cant seem to find the solution.

This is my script so far

local userInputService = game:GetService("UserInputService")




	userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	
	
	if input.UserInputType == Enum.UserInputType.Keyboard then
		
		if input.KeyCode == Enum.KeyCode.Insert then
			for loop = 1,10 do
				wait(0.001)
				script.Parent.BackgroundTransparency = loop/10
				game:GetService("RunService").Stepped:Wait()


			end
				end
			end
		end)
2 Likes

You just do it backward by -1.

for loop = 10, 1, -1 do
...

to make it fade in simply do
script.Parent.BackgroundTransparency = (-i + 10) / 10

also i would recommend that you use tweenservice

Instead of using loops, I would suggest using TweenService to have a smoother fade in.

2 Likes

maybe you go check on the community tutorial so that something that can help you with a problem. anyway maybe you try look at the video that i showing to you and if it is not work tell me.

I agree, use something like this:

local TweenService = game:GetService("TweenService")
local ScreenGui = script.Parent
local MainBackground = ScreenGui.Background

local fadeInTween = TweenService:Create(MainBackground, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0), {BackgroundTransparency = 0})
fadeInTween:Play()

--And optional:
local fadeOutTween = TweenService:Create(MainBackground, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0), {BackgroundTransparency = 1})
fadeOutTween:Play()
3 Likes