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

How to make a GUI Text fade in then out after a few seconds?

Hello, I'm trying to make a text fade into the screen after a number of seconds, then fade out. I was able to make a script to fade out, but I have no idea how to make it fade in first. I originally got the script from a video but I changed it up a bit, but now I'm stuck with this

script.Parent.Visible = false --I don't want it to pop up instantly so I set it to false
wait(5) --time it will be transparent until text pops up
script.Parent.Visible = true --this is where I'm stuck at, this instantly makes text pop up and I'm not sure how to make it fade in first
for loop = 1,10 do
 wait(0.1) --how much transparency it fades out by, 0.1 being the smoothest
 script.Parent.TextTransparency = loop/10 
 game:GetService("RunService").Stepped:Wait()
end

I'm not sure what to do next, I tried to search around with how to make it fade in but I couldn't find anything. The script is a LocalScript in a TextLabel. Any help is appreciated, thank you.

2 answers

in for loops, there is a third piece of information you can add to the loop which is the increment for example if you did

for i = 2, 4, 2 do
	print("hi")
end

the third number is the increment (if you don't set it, the default is 1) and it's number will be added to i each loop, making "hi" print twice

you can make the increment a negative number to count backwards in a for loop like so,

for i = 10, 1, -1 do
	print(i)
end

and this will print 10 counting backwards to 1

Hello!

So what we need to do here is create a function which will fade the property that you want, and you can do that by using this function:

local RunService = game:GetService("RunService")

local function fadeNumber(obj, property, v0, v1, t)
	coroutine.resume(coroutine.create(function()
	    local lastValue = obj[property]
	    local increment = 1/(t * 60)
	    for i = 0, 1, increment do
	        if obj[property] == lastValue then
	            obj[property] = v0 + i * (v1 - v0)
	            lastValue = obj[property]
	            RunService.RenderStepped:wait()
	        else
			obj[property] = v1
	            break
	        end
	    end
	end))
end

So how do we execute this?

local label = script.Parent
fadeNumber(label, "TextTransparency", 0, 1, 2)
wait(2)
fadeNumber(label, "TextTransparency", 1, 0, 2)

Function Arguments: obj - The object (e.g., TextLabel); property - The property (e.g., Transparency, TextTransparency); v0 - Starting value ("What number you want it to start on"); v1 - Goal value ("What number you want it to end up on"); t - The amount of time you want it to take

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