Custom loading script

Hey !

I’ve recently made a custom loading screen for my game.
I’m currently making everything work using functions, coroutine and loops until the game is loaded.

I’m just wondering if the script is well made or should i can/need improve it as it is the first time i’m doing it using a local script in Replicated First and first time using coroutine functions and “TypeWrite” code given on the dev hub to anim texts.

Video


Script


local Players = game:GetService("Players")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local TweenService = game:GetService("TweenService")

local Player = Players.LocalPlayer 
local PlayerGui = Player:WaitForChild("PlayerGui", 120)

local StopAnim = false

if PlayerGui ~= nil then
	local LoadingGui = PlayerGui:WaitForChild("Loading" ,60)

	if LoadingGui ~= nil then
		local Frame = LoadingGui:WaitForChild("Frame" ,30)

		if Frame ~= nil then
			local Animated = Frame:WaitForChild("Animated" ,30)

			if Animated ~= nil then
				local Circle = Animated:WaitForChild("Circle" ,30)
				local Hint = Animated:WaitForChild("Hint" ,30)
				local LoadText = Animated:WaitForChild("LoadText" ,30)
				local Logo = Animated:WaitForChild("Logo" ,30)
				local Background = Animated:WaitForChild("Background" ,30)

				---------------------------------

				local function RotateCircle()
					repeat
						Circle.Rotation = Circle.Rotation + 7
						wait(0.03)
					until
					StopAnim == true
				end

				local function TextFade()
					local TweenTime = TweenInfo.new(0.7)
					local Goal = {}
					local Invi = false

					repeat
						if Invi == false then
							Invi = true
							Goal.TextTransparency = 1
						elseif Invi == true then
							Invi = false
							Goal.TextTransparency = 0
						end

						local Tween = TweenService:Create(LoadText, TweenTime, Goal)						
						Tween:Play()					
						wait(0.7)
					until
					StopAnim == true
				end

				local function HintNext(Skip, Main)
					local Ready = true
					local Messages = {
						"- Teamplay is important in this game, let's find good team mates or friends to play with !",
						"- Upgrade your heroes to become stronger !",
						"- The heroes store is reseting every hours !",
						"- Kill the final boss to receive huge rewards !",
						"- A new update should be released every months !",
						"- Thanks for playing !",
					}

					Skip.MouseButton1Click:Connect(function()
						if Ready == true then
							Ready = false
							local Randoms = Messages[math.random(1,#Messages)]

							Randoms = Randoms:gsub("<br%s*/>", "\n")
							Randoms:gsub("<[^<>]->", "")
							Main.Text = Randoms

							local index = 0
							for first, last in utf8.graphemes(Randoms) do
								index = index + 1
								Main.MaxVisibleGraphemes = index
								wait(0.02)
							end
							wait(0.5)
							Ready = true
						end
					end)
				end

				local function LogoSize()
					local TweenTime = TweenInfo.new(1.25)
					local Goal = {}
					local Sized = 0

					repeat
						if Sized == 0 then
							Sized = 1
							Goal.Size = UDim2.new(0.217, 0, 0.231, 0)
							Goal.Position = UDim2.new(0.391, 0,0.069, 0)
						elseif Sized == 1 then
							Sized = 0
							Goal.Size = UDim2.new(0.197, 0, 0.211, 0)
							Goal.Position = UDim2.new(0.401, 0, 0.069, 0)
						end

						local Tween = TweenService:Create(Logo, TweenTime, Goal)						
						Tween:Play()					
						wait(1.25)
					until
					StopAnim == true
				end

				local function BackAnim()
					local TweenTime = TweenInfo.new(0.7)
					local Child = Background:GetChildren()
					local Goal = {}					

					repeat
						local Randoms = Child[math.random(1,#Child)]

						if Randoms:IsA("ImageLabel")then
							Goal.ImageTransparency = 0.35								
							local Tween1 = TweenService:Create(Randoms, TweenTime, Goal)	
							Tween1:Play()	
							
							wait(0.7)
							Goal.ImageTransparency = 0.85
							local Tween2 = TweenService:Create(Randoms, TweenTime, Goal)
							Tween2:Play()
						end
					until
					StopAnim == true
				end

				---------------------------------

				if Circle ~= nil then
					coroutine.resume(coroutine.create(function()
						RotateCircle()
					end))
				end

				if LoadText ~= nil then
					coroutine.resume(coroutine.create(function()
						TextFade()
					end))
				end

				if Hint ~= nil then
					local Skip = Hint:WaitForChild("Skip" ,30)
					local Main = Hint:WaitForChild("Main" ,30)

					if Skip ~= nil and Main ~= nil then
						coroutine.resume(coroutine.create(function()
							HintNext(Skip, Main)
						end))
					end
				end

				if Logo ~= nil then
					coroutine.resume(coroutine.create(function()
						LogoSize()
					end))
				end		

				if Background ~= nil then
					coroutine.resume(coroutine.create(function()
						BackAnim()
					end))
				end

				ReplicatedFirst:RemoveDefaultLoadingScreen()

				if not game:IsLoaded() then
					game.Loaded:Wait()
				end

				wait(60) --Just added to test and see the loading screen, as the game is loading too fast, will be reduced or deleted later			
				LoadingGui:Destroy()
				StopAnim = true
			end
		end
	end
end
2 Likes