Loading GUI cleanup

Here is code for a LocalScript that is for a loading GUI and when the loading completes it displays a list of players in the game. As you can see it is a mess of tweens and I don’t know if there’s a way to slim this down.

local replicatedFirst = game:GetService("ReplicatedFirst")
local replicatedStorage = game:GetService("ReplicatedStorage")
local contentProvider = game:GetService("ContentProvider")
local tweenService = game:GetService("TweenService")
local players = game:GetService("Players")

local queue = script.Parent
local background = queue:WaitForChild("background")
local timer = queue:WaitForChild("Timer")
local LOADINGPLAYERS = queue:WaitForChild("LOADING PLAYERS")
local LOADED = queue:WaitForChild("LOADED")
local scrollingFrame = queue:WaitForChild("playersLoadedFrame"):WaitForChild("ScrollingFrame")
local loadingFrame = queue:WaitForChild("loadingFrame")
local playersLoadedFrame = queue:WaitForChild("playersLoadedFrame")
local bar = playersLoadedFrame:WaitForChild("Bar")
local textBar = loadingFrame:WaitForChild("textBar")
local buttonBar = loadingFrame:WaitForChild("buttonBar")
local gameIsLoadingText = loadingFrame:WaitForChild("GameIsLoading")
local loadPercentage = loadingFrame:WaitForChild("loadPercentage")
local skipLoadButton = loadingFrame:WaitForChild("SkipLoad")
local loadingBarBackground = loadingFrame:WaitForChild("loadingBarBackground")
local loadingBar = loadingBarBackground:WaitForChild("loadingBar")

replicatedFirst:RemoveDefaultLoadingScreen()
loadingFrame.Visible = true
playersLoadedFrame.Transparency = 1
playersLoadedFrame:WaitForChild("Bar").ImageTransparency = 1
timer.TextTransparency = 1
LOADINGPLAYERS.TextTransparency = 1
LOADED.TextTransparency = 1

local assets = game.Workspace:GetDescendants()

local skipped = false

local closeTweenInfo = TweenInfo.new(1.7)
local openTweenInfo = TweenInfo.new(0.7)

function removeNotLoaded()
	skipLoadButton.Active = false
	local closeTween1 = tweenService:Create(background, closeTweenInfo, {BackgroundTransparency = 1})
	local closeTween2 = tweenService:Create(skipLoadButton, closeTweenInfo, {TextTransparency = 1})
	local closeTween3 = tweenService:Create(textBar, closeTweenInfo, {ImageTransparency = 1})
	local closeTween4 = tweenService:Create(buttonBar, closeTweenInfo, {ImageTransparency = 1})
	local closeTween5 = tweenService:Create(gameIsLoadingText, closeTweenInfo, {TextTransparency = 1})
	local closeTween6 = tweenService:Create(loadingBarBackground, closeTweenInfo, {BackgroundTransparency = 1})
	local closeTween7 = tweenService:Create(loadingBar, closeTweenInfo, {BackgroundTransparency = 1})
	local closeTween8 = tweenService:Create(loadPercentage, closeTweenInfo, {TextTransparency = 1})
	closeTween1:Play()
	closeTween2:Play()
	closeTween3:Play()
	closeTween4:Play()
	closeTween5:Play()
	closeTween6:Play()
	closeTween7:Play()
	closeTween8:Play()
	closeTween1.Completed:Wait()
	loadingFrame.Visible = false
	task.wait(.7)

	local openTween1 = tweenService:Create(playersLoadedFrame, openTweenInfo, {BackgroundTransparency = 0})
	local openTween2 = tweenService:Create(bar, openTweenInfo, {ImageTransparency = 0})
	local openTween3 = tweenService:Create(timer, openTweenInfo, {TextTransparency = 0})
	local openTween4 = tweenService:Create(LOADINGPLAYERS, openTweenInfo, {TextTransparency = 0})
	local openTween5 = tweenService:Create(LOADED, openTweenInfo, {TextTransparency = 0})
	openTween1:Play()
	openTween2:Play()
	openTween3:Play()
	openTween4:Play()
	openTween5:Play()
	openTween1.Completed:Wait()

	task.wait(.5)

	for _, v in pairs(players:GetPlayers()) do
		local template = replicatedStorage:WaitForChild("queueTemplate"):Clone()
		template.ImageLabel.Image = players:GetUserThumbnailAsync(v.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
		template.Name = v.Name
		template.Parent = scrollingFrame
	end
end

skipLoadButton.Activated:Connect(function()
	if loadingFrame.Visible == true then
		skipped = true
		removeNotLoaded()
	end
end)

for i = 1, #assets do
	local asset = assets[i]
	local percentage = math.round(i / #assets * 100)

	if skipped then
		break
	end

	contentProvider:PreloadAsync({asset})
	loadPercentage.Text = percentage.."%"

	tweenService:Create(loadingBar, TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Size = UDim2.fromScale(percentage / 100, 1)}):Play()

	if i % 50 == 0 then
		task.wait()
	end
end

if not skipped then
	loadPercentage.Text = "Game Loaded"
	task.wait(1)
	removeNotLoaded()
end

And here’s how it looks.

2 Likes

You can try to slim it down a little by adding :Play() at the back of the tweens.

For example:
tweenService:Create(skipLoadButton, closeTweenInfo, {TextTransparency = 1}):Play()

Or a more ideal way in my own opinion, is to create a tween function and call the function for each tween.

For example:

local TweenService = game:GetService("TweenService")
local workspace = game:GetService("Workspace")

local part1 = workspace.Part1
local part2 = workspace.Part2

local tweenInfo = TweenInfo.new(5)

-- Tween function
local function TweenInstance(instance: Instance, tweenInfo: TweenInfo, propertyTable: {[string]: any})
	local tween = TweenService:Create(instance, tweenInfo, propertyTable)
	
	tween:Play()
	
	tween.Completed:Wait()
end

-- Call the function
TweenInstance(part1, tweenInfo, {Position = workspace.SpawnLocation.Position})

TweenInstance(part2, tweenInfo, {Transparency = 0.5})

Of course you can change it to the way you like but that’s how i’d do it to not have multiple lines of code that do the same thing essentially.

Hope this helps!

3 Likes

I never thought of making a function for that. How convenient! Thanks.

2 Likes

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