The purpose of this script is to make the screen fade into black when a button is clicked. For some reason this does not happen and it confuses me. Could you please explain what I did wrong and how I could fix it?
function OnClicked() local x = Instance.new("Frame") x.Parent = script.Parent.Parent.ScreenGui x.BackgroundColor3 = Color3.new(0,0,0) x.Size = {1, 0},{1, 0} for i = 0 , 1, .1 do x.BackgroundTransparency = i wait(0.2) end x.Destroy = true end script.Parent.MouseButton1Down:connect(OnClicked)
You need to use BackgroundTransparency not Transparency
example
x.BackgroundTransparency = 0.1
and you also need to change
x = script.Parent.Parent.ScreenGui
to
x.Parent = script.Parent.Parent.ScreenGui
i also recommend using a loop rather than writing the same thing 10 times
example
for i = 0 , 1, .1 do x.BackgroundTransparency = i wait() end
your size is wrong you would need to type
x.Size = UDim2.new(xScale, xOffset, yScale, yOffset)
Your problem is that you are trying to change the transparency, when the value you want to change is BackgroundTransparency. Additionally, your code is very inefficient; you can replace that long reduntant piece of code with a loop.
the best loop to use here is a for loop, because they take little effort to make and are most commonly used when you want a counting variable
function OnClicked() local x = Instance.new("Frame") x = script.Parent.Parent.ScreenGui x.BackgroundColor3 = Color3.new(0,0,0) x.Size = UDim2.new(1,0,1,0) for i = 0, 1, .1 do x.BackgroundTransparency = i wait(0.2) end x:Destroy() end script.Parent.MouseButton1Down:connect(OnClicked)
The first parameter of a for loop is the variable you want to count, with the starting value. In this case, "i" is our counting variable, and we want it to start counting up from 0.
The second parameter is the goal you want to reach. The for loop will continuously run the given code until the counting variable reaches that goal.
The third parameter is the increment at which the counting variable counts at. The increment is added to the counting variable with each iteration of the for loop.
For more information on loops, you can search the roblox wiki here, or the scripting helpers glossary here.
Ok so I combined what both of you guys said and I fixed everything.
function OnClicked() local x = Instance.new("Frame") x.Parent = script.Parent.Parent.Parent x.BackgroundColor3 = Color3.new(0,0,0) x.Size = UDim2.new(1, 0, 1, 0) for i = 0, 1, .1 do x.BackgroundTransparency = i wait(0.2) end end script.Parent.MouseButton1Down:connect(OnClicked)