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

How to make text fade in and out?

I made a horror game and I want to add some text appear when you touch a block. Like you touch the block text fades in, wait 5 seconds then it fades out. I tryed this with step-by-step process but it won't work.

function onTouch()
	game.StarterGui.ScreenGui.Frame.Visible = true
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.9
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.8
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.7
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.6
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.5
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.4
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.3
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.2
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.1
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0
	wait (5)
        game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.1
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.2
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.3
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.4
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.5
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.6
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.7
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.8
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 0.9
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.TextLabel.Transparency = 1
	wait (0.5)
	game.StarterGui.ScreenGui.Frame.Visible = false
end
script.Parent.Touched:connect (onTouch)

Thx for the help!

1 comment

0

StarterGui is simply where stuff is cloned from. It can't change very player's gui,

3 answers

✓ Accepted answer

Solution

This solution has not been tested

You had some problems with your script. For example, you tried to get the gui from StarterGui. You should never do this, instead you must get the player from using :GetPlayerFromCharacter(). You would also want to use for i = 0, 1, 0.5 do or for i = 1, 0, -0.5 for doing a loop. It will make it smoother and it is better for fades instead of doing for i = 1, 10 do and so on.

One of the best ways of doing this is code below. If you don't understand any of it then please just let me know.

It is suggested that you copy this piece of code to your clipboard and paste it into studio so you can view it better.

script.Parent.Touched:connect(function(hit)
    local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent); -- we must somehow get the player
        if (hitplayer) then -- if what hit it was a player then
            local screengui = hitplayer.PlayerGui:FindFirstChild("ScreenGui"); -- defining the 'ScreenGui'
                if (screengui) then -- if there a is 'ScreenGui' then
                    screengui.Frame.Visible = true; -- making the frame visible
                        for i = 1, 0, -.05 do wait() -- Going from 1 to 0 at -0.05 per a wait()
                            screengui.Frame.TextLabel.TextTransparency = i; -- the tranparency is the current 'i'
                        end;
                        screengui.Frame.TextLabel.TextTransparency = 0; -- making sure it ends at 0
                        wait(5);
                        for i = 0, 1, .05 do wait() -- Going from 0 to 1 at .05 per a wait()
                            screengui.Frame.TextLabel.TextTransparency = i; -- the tranparency is the current 'i'
                        end;
		    screengui.Frame.TextLabel.TextTransparency = 1; -- making sure it ends at 1
                    screengui.Frame.Visible = false; -- making the frame invisible
                    end;
            end;
    end);

I hope you get what I'm trying to say, I haven't tested the script so I have no idea if it works. It should, but it just might not.

If this helped you then put it as the answer, if it didn't help then tell me why with a comment

4 comments

0

It doesn't work when I touch my invisible brick. Also do i need to make my frame visible or not?

0

The question is, what are you wanting to change? The TextTransparency or the BackgroundTransparency

0

Try it now, some syntax errors were fixed.

0

I wan't to make my text that I made fade in then after 5 sec to dissapear.

StarterGui is simply an object to clone Guis into every player's PlayerGui.

If you want a player's text to change:

local part = script.Parent

function fadeIn(text) -- makes text visible
	while text.TextTransparency > 0 do
		text.BackgroundTransparency = text.BackgroundTransparency - 0.1
		text.TextTransparency = text.TextTransparency - 0.1
		text.TextStrokeTransparency = text.TextStrokeTransparency - 0.1
		wait(0.1)
	end
end

function fadeOut(text) -- makes text iinvisible
	while text.TextTransparency < 1 do
		text.BackgroundTransparency = text.BackgroundTransparency + 0.1
		text.TextTransparency = text.TextTransparency + 0.1
		text.TextStrokeTransparency = text.TextStrokeTransparency + 0.1
		wait(0.1)
	end
end

local enabled = false

part.Touched:connect(function(hit)
	local p = game.Players:GetPlayerFromCharacter(hit.Parent) -- gets the player
	if p then -- checks to see if object that touched part is actually a player
		local gui = p.PlayerGui:FindFirstChild("GuiName")
		if gui then
			if enabled == false then -- makes sure fade isn't running
				enabled = true
				fadeIn(gui.Frame.Text)
				wait(5) -- text is visible for 5 seconds
				fadeOut(gui.Frame.Text)
				enabled = false
			end
		end
	end
end)

1 comment

0

You have unnecessary functions and your method of fading the text is also not the best way to do it.

Thx for the help! It worked! :)

script.Parent.Touched:connect(function(hit)
    local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent); -- we must somehow get the player
        if (hitplayer) then -- if what hit it was a player then
            local screengui = hitplayer.PlayerGui:FindFirstChild("ScreenGui"); -- defining the 'ScreenGui'
                if (screengui) then -- if there a is 'ScreenGui' then
                    screengui.Frame.Visible = true; -- making the frame visible
                        for i = 1, 0, -.05 do wait() -- Going from 1 to 0 at -0.05 per a wait()
                            screengui.Frame.TextLabel.TextTransparency = i; -- the tranparency is the current 'i'
                        end;
                        screengui.Frame.TextLabel.TextTransparency = 0; -- making sure it ends at 0
                        wait(5);
                        for i = 0, 1, .05 do wait() -- Going from 0 to 1 at .05 per a wait()
                            screengui.Frame.TextLabel.TextTransparency = i; -- the tranparency is the current 'i'
                        end;
            screengui.Frame.TextLabel.TextTransparency = 1; -- making sure it ends at 1
                    screengui.Frame.Visible = false; -- making the frame invisible
                    end;
            end;
    end);

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