Loading Screen Local Script Review

This code does work, but I feel like it could definitely be improved. This is for a loading screen that will show the assets being loaded in, and then once completed, it will fade out and show a menu screen. Any tips?

local ContentProvider = game:GetService("ContentProvider")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local StarterGui = game:GetService("StarterGui")
local success

local text = script.Parent

local loadInstances = workspace:GetDescendants()
local cameraAngles = workspace:FindFirstChild("CameraAngles")

local player = game:GetService("Players").LocalPlayer
local respawnEvent = game:GetService("ReplicatedStorage").RespawnEvent

local mainMenu = script.Parent.Parent.MainMenu
mainMenu.Visible = false

script.Parent.Visible = true

respawnEvent:FireServer(workspace:FindFirstChild("LoadingPart"),false)

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All,false)

repeat
	RunService.Heartbeat:Wait()
	success = pcall(StarterGui.SetCore,StarterGui,"ResetButtonCallback",false)
until success

for i=1,#loadInstances do
	local loading = loadInstances[i]
	ContentProvider:PreloadAsync({loading})
	
	text.Text = tostring(math.floor((i/#loadInstances)*100)).."%\n"..tostring(i).."/"..tostring(#loadInstances)
end

local randomAngle = cameraAngles:GetChildren()[math.random(1,#cameraAngles:GetChildren())]
local currentCam = workspace.CurrentCamera
currentCam.CameraType = Enum.CameraType.Scriptable
local fov = randomAngle:GetAttribute("FOV")
if fov then currentCam.FieldOfView = fov end
currentCam.CFrame = randomAngle.CFrame

task.wait(1)

mainMenu.Visible = true
local tweenOut = TweenService:Create(text,TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out),{BackgroundTransparency = 1,TextTransparency = 1})
tweenOut:Play()

In the documentation for ContentProvider:PreloadAsync(), it says that the engine iterates through any asset that uses a link, such as an image or sound. In your script, you’re iterating through the entire workspace, which means you will have assets that don’t use content links. This slows down your loading a lot because it causes the service to run additional checks for the links.

When your client application joins a server and you’re shown the default Roblox loading screen, that loading process is prioritizing prefabricated assets (Roblox default parts, lights, and user interface). It’s also based on network latency between your location and the server’s location, which is important to note because using ContentProvider:PreloadAsync() on high priority parts is causing additional load-times, since you’re running another layer of loading.

So it’s best to only preload anything with links, you can filter out your entire workspace with checks, but a smart way is to organize all of your assets with content links into a folder tree. This will increase iteration efficiency because the engine can process a much smaller array of data.

1 Like