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.
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.
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
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
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
Seems like the problem you are having is the screen not fading to black after an animation plays.
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.
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.
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!