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

How can I make the screen fade to black after a certain amount of time?

I've tried making this work, but I can find nothing. I scripted an animation to play when the user joins and I want the screen to turn black after it finishes and I know that I need to do a wait() command but I just don't know exactly how to script the BackgroundTransparency properly to black. I hope this does not sound like a request I just need some sort of understanding and help.

1 comment

0

Can I reword? I want a black screen with the play button to show up when the animation finishes playing

5 answers

✓ Accepted answer

You can either use the for loop method or you can use tweening. I find tweening the best when it comes to fading in and other animations in terms of GUI's.

local tweenservice = game:GetService("TweenService")
local fadebackground = script.Parent --change this to where your background is

local tweenInfo = TweenInfo.new(
	0.5,--time in seconds
	Enum.EasingStyle.Sine, --smooth
	Enum.EasingDirection.Out, --dont change
	0, -- dont change
	true, --reverse
	0
)

local tween = tweenservice:Create(fadebackground, tweenInfo, {BackgroundTransparency = 0})
tween:Play()

Make sure to accept this answer.

2 comments

0

This is also way smoother than the for loop method.

0

Ah nice. Would have covered this in my answer but I know little about tweenService as I rarely use it.

make a new frame that covers the other one set backround transparency to 1 set color to black

that might work

local frame= --the frame you added 


wait ()-the time you what 
 local i = 1

while i<=0  do 
i=i-0.1
frame.BackroundTransparency=i
wait (0.1)


end


2 comments

0

What do you mean in "the frame you added"

0

you need to add a new frame that covers the other frame and set color to black then set backround transparency to 1

tylergoatboy5's answer is valid, but I believe there's an easier way to do this. Just use a for loop.


-- Create the frame that will fade to black
local gui = Instance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui)
local frame = Instance.new("Frame", gui)
frame.Size = UDim2.new(1,0,1,0)
frame.BackgroundColor3 = Color3.new(0,0,0)

-- Make it fade to black
for a = 1, 0, -0.1 do -- This will count down by 0.1 until a is equal to 0.
	frame.BackgroundTransparency = a 
	wait(0.1)
end

5 comments

0

Side note, this has to be in a localscript for this to work. If it's not in a localscript, please post the rest of the script and I will adjust my answer so that it works.

0

This script just makes it gray right when I click Play?

0

One issue with the for loop here. You're going backwards from 1 - 0 but incrementally moving forward.

0

Oh snap AZDev, you right. Also, mathinator, is that a question or a statement?

0

Statement

There are a couple of ways you can do this.

We can use linear interpolation

local function Lerp(Start, Finish, Alpha)
	return (1 - Alpha) * Start + Alpha * Finish
end

-- Start is the number that we start at. So for transparency
-- Start = 1
-- Finish = 0
-- Alpha is a number between 0 and 1.
	-- 0 being no progress
	-- 1 being the end.
	-- Think of it like this 0 = 0% complete, 1 = 100% complete.

How do we use this in our code? There are a few ways but I'll cover the most basic.

-- loop from 0 to 1 in increments of 0.1 for a total of 10 steps.
for Alpha = 0, 1, 0.1 do
	YourObject.BackgroundTrasparency = Lerp(1, 0, Alpha)
	wait()
end

Now keep in mind, if you don't intend to use the lerp function to create a time based animation, it's probably not worth the effort or the added computation needed to solve lerp.

Without the lerp function we can simply do this.

for i = 1, 0, -0.1 do
	YourObject.BackgroundTransparency = i
	wait()
end
-- Then flip to fade out.
for i = 0, 1, 0.1 do
	YourObject.BackgroundTransparency = i
	wait()
end

4 comments

0

Thanks for breaking it down, but somehow I still do not understand. I know near to nothing about scripting...

0

Do you have any more you can help me with in explaining how to do this?

0

This is pretty straight-forward. What part do you not understand? I'll try and explain it further.

0

I do not understand how to implement it into my script. I feel like my way would work and at this point, to make it easier for me, I don't really care whether it fades in or not I just want the grey/black screen to popup so you can't see anything else but the play button.

Hello, MathinatorMatthew, I'm TheOnlySmarts and I will be able to help you with your scripting problem. I wouldn't of actually helped you but since you can't understand others scripting, I will make this as easy as possible.


Problem

Seems like the problem you are having is the screen not fading to black after an animation plays.

Information and Tips

This can easily be fixed/done with loops, also, it is not possible to access your GUI through StarterGui. You'd have to use PlayerGui. Don't worry. I'll go over that in a second.

Solution - StarterGui or PlayerGui

StarterGui is a little bit hard to explain, ROBLOX Studio made it so it changes to PlayerGui through out the Start of the game, hence the meaning out of the name, 'StarterGui'. But we can't just change it to game.PlayerGui as the PlayerGui is in our Player, that's why it's called, 'PlayerGui'.

We can find the PlayerGui 2 different ways. Involving 2 different scripts.

The Script and the LocalScript.

Lets find it with the Script first.

game.Players.PlayerAdded:Connect(function(player) -- Player joined
	player.PlayerGui.etc.etc.etc -- We have just found the PlayerGui with 3 lines of simple code!
end)

Now lets find it with the LocalScript.

game.Players.PlayerAdded:Connect(function(player)
	game.Players.LocalPlayer.PlayerGui.etc.etc.etc -- We can use the player.PlayerGui, but in this case, just to mix it up, we'll be using the LocalPlayer
end)

Hope this helped, now lets move on to your other issue, loops.

Solution - loops

Loops are one of the most annoying problems to deal with when a beginner to scripting. They're easy to learn, but look so somewhat hard to learn. Some people who want to script even give up because of loops. But, they're really easy to learn!

We can use 3 loops, the classic wait() loop, the for loop, and the while loop.

In this scenario, we'll be using the for loop, a simple loop that is easily learnt.

Now before I get into just showing you a script of using the for loop, obviously I'm going to explain about it. To make this as easy as possible.

So, for loops are commonly used when adding a value into a number. Or subtracting a number. That is why you mostly see numbers and subtraction signs when people script with a for loop.

Usually it'd look like this.

for timecount = 10, 0, -1 do
	print("hi")
end

And that script would print hi 1 time every second until it's counted it 10 times. And that script may look a little bit experted for newbies, but don't worry. After you get the hang of it, it's really not.

Well, now that you know about for loops, PlayerAdded functions, all of that. You're finally up to your last 3-5 lines of words.

Here is your script modified.

game.Players.PlayerAdded:Connect(function(player) -- Identifying the Player joined.
	for timecount = 1, 0, -0.1 do -- Adding 0.1 Transparency
		player.PlayerGui.GuiHere.FrameHere.BackgroundTransparency = timecount -- We're matching it up with timecount.
	end
	if player.PlayerGui.GuiHere.FrameHere.BackgroundTransparency = 0 then -- If it matches to our value
		print("Works") -- Debugging it to see if it works and that there is no errors
	end
end)

Well, hopefully this helped, enjoy creating your game!


Tip : If this Answer has helped you in a sort of way, it's recommended to click Accept Answer, letting the Author know that it has helped you. It also gives you both positive reputation!

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