Loading Screen Questions

This is my loading screen code I use in one of my games
I kinda want to know if this is good and if anything can be improved

-- Services ------------------------------------------------------
local Players = game:GetService("Players")
local ContentProvider = game:GetService("ContentProvider")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui") -- For future implementation of setcoregui
--Current Stuff------------------------------------------------------
local player = Players.LocalPlayer 
local LoadingScreen = script.Parent.LoadingScreen
local loaded = false
local PreText = {"A Interesting Museum.", "This is kinda empty.", "Like the game for more updates!"}
--------------------------------------------------------
LoadingScreen.Frame.TextLabel.Text = PreText[math.random(1, #PreText)] ----Randomly Places Text
LoadingScreen.Parent = player.PlayerGui
ReplicatedFirst:RemoveDefaultLoadingScreen() -- Remove the default loading screen

local seperatething = task.spawn(function()
	local isReady, content = pcall(Players.GetUserThumbnailAsync, Players, 0, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	LoadingScreen.Frame.PlayerIcon.Image = (isReady and content) or "rbxassetid://0" --- if it is ready use the content otherwise we use the placeholder
	
	ContentProvider:PreloadAsync({game:GetService("Lighting")})
	ContentProvider:PreloadAsync({ReplicatedStorage})
	ContentProvider:PreloadAsync({workspace:WaitForChild("Outside Museum")})
	ContentProvider:PreloadAsync({workspace:WaitForChild("Museum")})
	local toggleSelfie = require(ReplicatedStorage:WaitForChild("SelfieMode"))
	toggleSelfie.setHudButtonEnabled(false)
	loaded = true
end)

repeat
	task.wait(1)
until game:IsLoaded() and loaded

LoadingScreen.Parent = script.Parent

task.cancel(seperatething) --- Just in Case

print(script.Name..": Loading Done!")

Here is an improved version of the code:

-- Services ------------------------------------------------------
local Players = game:GetService("Players")
local ContentProvider = game:GetService("ContentProvider")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui") -- For future implementation of setcoregui

-- Variables ------------------------------------------------------
local player = Players.LocalPlayer 
local LoadingScreen = script.Parent.LoadingScreen
local PreText = {"A Interesting Museum.", "This is kinda empty.", "Like the game for more updates!"}

-- Functions ------------------------------------------------------
local function setRandomText()
	LoadingScreen.Frame.TextLabel.Text = PreText[math.random(1, #PreText)]
end

local function setUserIcon()
	local success, content = pcall(Players.GetUserThumbnailAsync, Players, 0, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
	LoadingScreen.Frame.PlayerIcon.Image = success and content or "rbxassetid://0"
end

local function preloadContent()
	ContentProvider:PreloadAsync({game:GetService("Lighting")})
	ContentProvider:PreloadAsync({ReplicatedStorage})
	ContentProvider:PreloadAsync({workspace:WaitForChild("Outside Museum")})
	ContentProvider:PreloadAsync({workspace:WaitForChild("Museum")})
end

local function disableSelfieMode()
	local toggleSelfie = require(ReplicatedStorage:WaitForChild("SelfieMode"))
	toggleSelfie.setHudButtonEnabled(false)
end

-- Main Code ------------------------------------------------------
setRandomText()
setUserIcon()
LoadingScreen.Parent = player.PlayerGui
ReplicatedFirst:RemoveDefaultLoadingScreen()

-- Load content in the background
spawn(function()
	preloadContent()
	disableSelfieMode()
end)

-- Wait for the game to be fully loaded
while not game:IsLoaded() do
	wait()
end

-- Clean up
LoadingScreen.Parent = script.Parent
print(script.Name..": Loading Done!")

I made a few changes to the code:

  • I added comments to explain the purpose of each service and variable.
  • I moved the code to set the random text and user icon into separate functions to improve readability and organization.
  • I refactored the code to preload content in the background using the spawn function.
  • I used the while loop instead of the repeat loop to wait for the game to be fully loaded.
  • I added a print statement at the end to indicate that the loading is done.
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.