Can't seem to get a loading screen to fade out once loaded

Hello Developers,

I’ve been in studio today, and have tried to create a loading screen that fades out when the game is loaded. I already have the loading part down, but I can’t seem to get the screen to fade out. So I’ve come here for help! Like I said above, i’m trying to get the screen to fade out when the game is loaded.

I have re-written the script about 5 times, and still can’t seem to get it to work. So at this point i’m in need for help. Below is my code block, please tell me what I should add, or do to get the screen to fade out.

local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local LoadingScreenGui = game.ReplicatedFirst.LoadingScreenGui
LoadingScreenGui.Parent = PlayerGui
local i = 0

ReplicatedFirst:RemoveDefaultLoadingScreen()
wait(5)
if not game:IsLoaded() then
game.Loaded:Wait()
if game.Loaded == true then
while wait(.1) do
	i = i + .05
LoadingScreenGui.MainFrame.BackgroundTransparency = i
end
end
end
LoadingScreenGui:Destroy()

Thank you for your help!

1 Like

I would just do a for i loop instead, as shown here:

local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Player = Players.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")
local LoadingScreenGui = game.ReplicatedFirst.LoadingScreenGui
LoadingScreenGui.Parent = PlayerGui

ReplicatedFirst:RemoveDefaultLoadingScreen()
wait(5)
if not game:IsLoaded() then
    game.Loaded:Wait()
elseif game.Loaded == true then
    for i = 0,1,0.05 do --goes from 0 to 1 in increments of 0.05
        LoadingScreenGui.MainFrame.BackgroundTransparency = i
        wait()
    end
    LoadingScreenGui:Destroy()
end
1 Like

Maybe it is because you are destroying the gui right after, so maybe do something like

    '''

        if LoadingScreenGui.MainFrame.BackgroundTransperency == 1 then
  
          '''

Put this after 2 lines after the LoadingScreenGui.MainFrame.BackgroundTransperency = i part
(Also do what @OIogist said)

1 Like

You shouldn’t use a while loop, as that is a condition-controlled loop (will continue until x is x).

It wouldn’t ever get deleted as the loop’s going to continue forever so any code after it will be ignored until it’s over (in this case, it won’t ever get run)

while wait(.1) do
	i = i + .05
LoadingScreenGui.MainFrame.BackgroundTransparency = i -- within the loop
end -- end of loop
-- out of loop

Instead, you’re better off using a count-controlled loop as you know how many reiterations it will do. @Ologist’s solution should work, and the indentations are a lot clearer. Sometimes strange indentations can throw you off.

1 Like

Did not work

0000000000000000000

What issue are you having? Is it not fading? More information on what doesn’t work would be useful.

Please be more clear. A screenshot or video of what isn’t working right, along with any output errors, would be nice.

It just dissapears once loaded, no different result

I’d just tween it…
local tweenService = game:GetService(“TweenService”)
local frame = game.ReplicatedFirst.LoadingScreenGui.MainFrame
local change = {BackgroundTransparency = 1}
local info = TweenInfo.new(3) – time it takes
local tween = tweenService:Create(frame, info, change)

1 Like

Like @c7c said, best way to do it is with tween.

1 Like

But tweening is simple, and i’d like to fade it

Just use a repeat loop

repeat wait() until game:IsLoaded() == true do
print("game is loaded")

Also, for the fade loop don’t use a while loop because it won’t stop unless you break it or return it.

for i = 1, 10 do
   wait()
   something.BackgroundTransparency = i/10
end
1 Like

or

game.Loaded:Connect(function()
        print'Loaded'
end)
1 Like