Large delay in scripts

I started to rewrite my game as I messed up from the beginning, but somehow certain things in one of my GUI are really delayed.
I got 2 scripts right now, one for the loading screen and one for the main menu,
The problem? Buttons, Tweens and some function only start working after some seconds.

The Main Menu Script: (Stored in StarterPlayerScripts)



--━━━━━━━━━━━━━━━━━━━━━━━━━━━| Variables |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
local wfc		=game.WaitForChild
local ffc		=game.FindFirstChild
local player	=game.Players.LocalPlayer

local playerteam		=player.Team.Name

local TweenService		=game:GetService("TweenService")
local UserInputService	=game:GetService("UserInputService")
local Workspace			=game:GetService("Workspace")
local Players			=game:GetService("Players")
local Lighting 			=game:GetService("Lighting")
local ReplicatedFirst	=game:GetService("ReplicatedFirst")
local ReplicatedStorage	=game:GetService("ReplicatedStorage")
local SpawnWeaponRE			=wfc(ReplicatedStorage,"SpawnWeaponRE")
local ChangeTeam			=wfc(ReplicatedStorage,"ChangeTeam")
local GunsWS				=wfc(ReplicatedStorage,"GunsWS")
local TeamCLS					=wfc(GunsWS,playerteam).Value
local CatTeamCLS				=wfc(GunsWS,"Cat union").Value
local DogTeamCLS				=wfc(GunsWS,"Dog union").Value
local StarterGui		=game:GetService("StarterGui")
local StarterPack		=game:GetService("StarterPack")
local StarterPlayer		=game:GetService("StarterPlayer")
local Teams				=game:GetService("Teams")
local Catunion 				=wfc(Teams,"Cat union").name
local Dogunion 				=wfc(Teams,"Dog union").name
local Spectators 			=wfc(Teams,"Spectators").name
local SoundService		=game:GetService("SoundService")
local ClickSound			=wfc(SoundService,"ClickSound")
local ErrorSound			=wfc(SoundService,"XError")
local LobbyMusic			=wfc(SoundService,"LobbyMusic")

local TextChatService 	=game:GetService("TextChatService")

local PlayerGui	=wfc(player,"PlayerGui")
repeat wait() until ffc(PlayerGui,"MainMenu")

local LoadoutGui	=wfc(PlayerGui,"LoadoutGui")
local loadout 			=wfc(LoadoutGui,"Loadout")
local DevLogScreen	=wfc(PlayerGui,"DevLogScreen")
local SettingsScreen=wfc(PlayerGui,"SettingsScreen")
local MainMenu		=wfc(PlayerGui,"MainMenu")
local GameTitle 		=wfc(MainMenu,"GameTitle")
local PlayButtonFrame	=wfc(MainMenu,"PlayButtonFrame")
local PlayButton			=wfc(PlayButtonFrame,"PlayButton")
local LoadoutButtonFrame=wfc(MainMenu,"LoadoutButtonFrame")
local ToggleLoadoutGui		=wfc(LoadoutButtonFrame,"ToggleLoadoutGui")
local TeamChangeButtonFrame	=wfc(MainMenu,"TeamChangeButtonFrame")
local TeamChangeButton			=wfc(TeamChangeButtonFrame,"TeamChangeButton")
local SettingsButtonFrame	=wfc(MainMenu,"SettingsButtonFrame")
local SettingsButton		=wfc(SettingsButtonFrame,"SettingsButton")

local ButtonGradient		={
	wfc(PlayButtonFrame,"UIGradient"),
	wfc(PlayButton,"UIGradient"),
	wfc(LoadoutButtonFrame,"UIGradient"),
	wfc(ToggleLoadoutGui,"UIGradient"),
	wfc(TeamChangeButtonFrame,"UIGradient"),
	wfc(TeamChangeButton,"UIGradient"),
	wfc(SettingsButtonFrame,"UIGradient"),
	wfc(SettingsButton,"UIGradient")}
local ButtonFrames			={
	PlayButtonFrame,
	LoadoutButtonFrame,
	TeamChangeButtonFrame,
	SettingsButtonFrame}

--━━━━━━━━━━━━━━━━━━━━━━━━━━━| SETUP |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
LobbyMusic.Volume = 0
TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
local LMT = TweenService:create(LobbyMusic, TweenInfo.new(100), {Volume = 10})
local function MenuON()
	MainMenu.Enabled = true
	LMT:Play()
	LobbyMusic:Play()
	LMT.Completed:Wait()
end
player.CharacterAdded:Connect(MenuON)
--task.spawn(MenuON)

local function Play()
	if playerteam == Spectators then
		PlayButton.Text		="Please Change Your Team"
		ErrorSound:Play()
		wait(3)
		PlayButton.Text		="Play"
	else
		SpawnWeaponRE:FireServer(player)
		MainMenu:Destroy()
		LobbyMusic.Volume = 0
		ClickSound:Play()
	end
end

PlayButton.MouseButton1Down:Connect(function()
	Play()
end)
--━━━━━━━━━━━━━━━━━━━━━━━━━━━| GUI ANIMATION |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
local CTweenInfo	=TweenInfo.new(.8, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut, 0, false, 0)
local STweenInfo	=TweenInfo.new(.8, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut, 0, false, 0)
local Offset1 = {Offset = Vector2.new(1,0)}
local Offset2 = {Offset = Vector2.new(0,0)}

for i,btn in pairs(ButtonFrames) do
	for	i,v in pairs(ButtonGradient) do
		btn.MouseEnter:Connect(function()
			local CTween =TweenService:create(v, CTweenInfo, Offset1)
			CTween:Play()
			CTween.Completed:Wait()
		end)
		btn.MouseLeave:Connect(function()
			local CTween =TweenService:create(v, CTweenInfo, Offset2)
			CTween:Play()
			CTween.Completed:Wait()
		end)
	end
	for i,v in pairs(ButtonFrames) do
		btn.MouseEnter:Connect(function()
			local STween =TweenService:create(v, STweenInfo, {Size = UDim2.new(0.26, 0, 0, 45)})
			STween:Play()
			STween.Completed:Wait()
		end)
		btn.MouseLeave:Connect(function()
			local STween =TweenService:create(v, STweenInfo, {Size = UDim2.new(0.25, 0, 0, 35)})
			STween:Play()
			STween.Completed:Wait()
		end)
	end
end

--━━━━━━━━━━━━━━━━━━━━━━━━━━━| TEAMCHANGE |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
TeamChangeButton.MouseButton1Down:Connect(function()
	if playerteam == Spectators  or playerteam == Catunion and TeamCLS <= DogTeamCLS or playerteam == Dogunion and TeamCLS <= CatTeamCLS then
		ChangeTeam:FireServer(player)
		wait(0.1)
		TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
	elseif playerteam == Catunion and TeamCLS > DogTeamCLS then
		TeamChangeButton.Text = Dogunion.."team is full"
		wait(3)
		TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
	elseif  playerteam == Dogunion and TeamCLS > CatTeamCLS then
		TeamChangeButton.Text = Catunion.."team is full"
		wait(3)
		TeamChangeButton.Text = "Your Team:\n"..playerteam.."\nClick to Change Team"
	end
end)

--━━━━━━━━━━━━━━━━━━━━━━━━━━━| LOADOUT |━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━--
loadout.Visible = false
ToggleLoadoutGui.MouseButton1Down:Connect(function()
	if loadout.Visible == true then
		loadout.Visible = false
		PlayButton.Visible = true
		TeamChangeButton.Visible = true
		GameTitle.Visible = true
	else if loadout.Visible == false then
			loadout.Visible = true
			PlayButton.Visible = false
			TeamChangeButton.Visible = false
			GameTitle.Visible = false
		end
	end
end)

The Loading Script:

local wfc		=game.WaitForChild
local ffc		=game.FindFirstChild
local player	=game.Players.LocalPlayer
local TweenService		=game:GetService("TweenService")
local UserInputService	=game:GetService("UserInputService")
local Workspace			=game:GetService("Workspace")
local Players			=game:GetService("Players")
local Lighting 			=game:GetService("Lighting")
local ReplicatedFirst	=game:GetService("ReplicatedFirst")
local ReplicatedStorage	=game:GetService("ReplicatedStorage")
local GUIS					=wfc(ReplicatedStorage,"GUIS")
local MainMenu					=wfc(GUIS,"MainMenu")
local StarterGui		=game:GetService("StarterGui")
local StarterPack		=game:GetService("StarterPack")
local StarterPlayer		=game:GetService("StarterPlayer")
local Teams				=game:GetService("Teams")
local SoundService		=game:GetService("SoundService")
local LobbyMusic			=wfc(SoundService,"LobbyMusic")
local LoadingMusic			=wfc(SoundService,"LoadingMusic")
local TextChatService 	=game:GetService("TextChatService")
local PlayerGui			=wfc(player,"PlayerGui")
script.Parent.Parent	=PlayerGui
local LoadingScreen			=wfc(PlayerGui,"LoadingScreen")
local LoadScreenFrame			=wfc(LoadingScreen,"Frame")
local ImgLabel						=wfc(LoadScreenFrame,"ImageLabel")
local Background					=wfc(LoadScreenFrame,"Background")
local LoadingText					=wfc(LoadScreenFrame,"Loading")
local GameTitle						=wfc(LoadScreenFrame,"Title")

GameTitle.TextTransparency = 1

local Img1			="rbxassetid://16342495496"
local Img2			="rbxassetid://16342495759"
local Img3			="rbxassetid://16342495223"
local Img4			="rbxassetid://16342495001"
local Img5			="rbxassetid://16342494800"
local Info		=TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, true, 0)
local SNDInfo	=TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)
local Tween 	=TweenService:Create(ImgLabel, TweenInfo.new(20, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, -1, false), {Rotation = -360})
local LMusic 	=TweenService:create(LoadingMusic, TweenInfo.new(50), {Volume = .5})
Tween:Play()
LMusic:Play()
LoadingMusic:Play()

local function updateLoadingText()
	while true do
		LoadingText.Text = "Loading"
		wait(0.5)
		LoadingText.Text = "Loading ."
		wait(0.5)
		LoadingText.Text = "Loading . ."
		wait(0.5)
		LoadingText.Text = "Loading . . ."
		wait(0.5)
	end
end
spawn(updateLoadingText)
local function FadeInOut()
	local Tween				=TweenService:Create(Background, Info, {ImageTransparency = 0})
	Tween:Play()
	Tween.Completed:Wait()
	local Tween				=TweenService:Create(Background, Info, {ImageTransparency = 1})
	Tween:Play()
	wait(.1)
end
local function FadeGameTitle()
	local Tween				=TweenService:Create(GameTitle, Info, {TextTransparency = 0})
	Tween:Play()
	Tween.Completed:Wait()
	local Tween				=TweenService:Create(GameTitle, Info, {TextTransparency = 1})
	Tween:Play()
end
local function CloneGUIS()
	MainMenu.Enabled = true
	for i,v in pairs(GUIS:GetChildren()) do
		v.Parent = StarterGui
		v:Clone().Parent= PlayerGui
	end
end

Background.Image = Img1
FadeInOut()
Background.Image = Img2
FadeInOut()
Background.Image = Img3
FadeInOut()
Background.Image = Img4
FadeInOut()
spawn(CloneGUIS)
Background.Image = Img5
spawn(FadeGameTitle)
FadeInOut()

for i,v in pairs (LoadingScreen:GetDescendants()) do
	if v:IsA("ImageLabel") then
		local Tween	=TweenService:Create(v, SNDInfo, {BackgroundTransparency = 1, ImageTransparency = 1})

		Tween:Play()
	elseif v:IsA("TextLabel") then
		local Tween	=TweenService:Create(v, SNDInfo, {BackgroundTransparency = 1, TextTransparency = 1})
		Tween:Play()
	elseif v:IsA("Frame") then
		local Tween	=TweenService:Create(v, SNDInfo, {BackgroundTransparency = 1})
		Tween:Play()
	elseif v:IsA("UIStroke") then
		local Tween =TweenService:Create(v, SNDInfo, {Transparency = 1})
	Tween:Play()
	end
end

repeat wait() until Background.ImageTransparency == 1 and LoadingText.TextTransparency == 1

if LoadingMusic.Volume > 0 then
	local LMusic = TweenService:create(LoadingMusic, TweenInfo.new(5), {Volume = 0})
	LMusic:Play()
	LMusic.Completed:Wait()
	LoadingMusic:Destroy()
end

local LMT = TweenService:create(LobbyMusic, TweenInfo.new(100), {Volume = 10})
LMT:Play()
LobbyMusic:Play()
wait(5)
script.Parent:Destroy()


here is a video that it only starts working after a certain amount of seconds

1 Like

fixed it, guis were overlapping each other so I added a bit of code

repeat wait() until Background.ImageTransparency >= .8 and LoadingText.TextTransparency >= .8
LoadScreenFrame.Active = false

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