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()
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
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.
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)