How could I improve my Intro Script?

How could I improve my Script so the same can be even more powerful and optmized? It is a LocalScript inside of a frame that is inside of a ScreenGUI called “Menu”. What could I do to make that readeable and powerful? Thank you in advance.

local Gui = script.Parent
local plr = game.Players.LocalPlayer
local TS = game:GetService("TweenService")
local tweens = TS:Create(Gui.BlackFrame.WhiteFrame,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{Transparency = 0})
local uses,maxuses = 0,1
local used = false
local TrueMenu = Gui.TrueMenu
local rgb = Color3.fromRGB



local function PlaySound(sound)
	sound:Play()
end

for _,v in pairs(Gui:GetDescendants()) do
	if v.ClassName == "TextButton" then
		v.MouseEnter:Connect(function() -- When mouse Enter Function
			while v.MaxVisibleGraphemes == 0 do wait() end

			PlaySound(script.Text2)
      		local dt = Gui.BlackFrame.Determination
			v.TextColor3 = rgb(255, 247, 22)
			v.BorderColor3 = rgb(255, 247, 22)
			dt.Parent = v 
			dt.ImageTransparency = 0
		end)
		v.MouseLeave:Connect(function()
			while v.MaxVisibleGraphemes == 0 do wait() end

			v.TextColor3 = rgb(255,255,255)
			v.BorderColor3 = rgb(255,255,255)
			local dt = v:WaitForChild("Determination", 3) 
			dt.ImageTransparency = 1 
			dt.Parent = Gui.BlackFrame
		end)
		v.MouseButton1Click:Connect(function()
			if TrueMenu.Value == false then
				while used == false do
					used = true
					local Debris = game:GetService("Debris")
					local textbox = Gui:WaitForChild("Do not Be Silly, Kiddo."):Clone()
					textbox.Parent = Gui.BlackFrame
					Debris:AddItem(textbox,1.7)
					for i = 1, string.len(textbox.Text) do
						textbox.MaxVisibleGraphemes = i
						task.wait(0.05)
					end
					used = false
					return
				end
				return
			end
			while v.MaxVisibleGraphemes == 0 do wait() end
			if uses == maxuses then return end
			uses += 1
			print("Used in "..tostring(v).."! Have fun Player!")
			local sound1,sound2,sound3 = script.Text3,script.Text4,script.Text5
			if v.Name == "Play" then
				PlaySound(sound1)
			elseif v.Name == "Credits" then
				PlaySound(sound2)
			elseif v.Name == "Tutorial" then
				PlaySound(sound3)
			end
			tweens:Play()
			PlaySound(script.Text6)
			wait(6.8)
			Gui.Enabled = false
			wait(2)
			Gui.Parent:Destroy()
		end)
	end
end
8 Likes

This function doesn’t really serve much use since its only one line of code lol

5 Likes

Lol true I was thinking about that.

3 Likes

I would just use more functions to make sure everything is clear and understandable. What @DuckDankz said isn’t necessarily true. If you think it is more appealing to use a “PlaySound” function, you can totally do so. It’s all about style. I would also put everything together, this includes: Services, variables, functions …

Here is what i came up with, but you can always change it however you want. I don’t think there is any optimization work needed.

--// SERVICES //--
local Debris = game:GetService("Debris")
local TS = game:GetService("TweenService")
local Players = game:GetService("Players")

--// PLAYER //--
local plr = Players.LocalPlayer

--// GUI //--
local Gui = script.Parent
local TrueMenu = Gui.TrueMenu

--// VARIABLES & OPTIMIZERS //--
local tweens = TS:Create(Gui.BlackFrame.WhiteFrame,TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0),{Transparency = 0})
local uses,maxuses = 0,1
local used = false
local rgb = Color3.fromRGB

--// FUNCTIONS //--
local function PlaySound(sound)
	sound:Play()
end

local function MouseEnter(object)
	while object.MaxVisibleGraphemes == 0 do wait() end

	PlaySound(script.Text2)
	
	local dt = Gui.BlackFrame.Determination
	object.TextColor3 = rgb(255, 247, 22)
	object.BorderColor3 = rgb(255, 247, 22)
	dt.Parent = object
	dt.ImageTransparency = 0
end

local function MouseLeave(object)
	while object.MaxVisibleGraphemes == 0 do wait() end

	object.TextColor3 = rgb(255,255,255)
	object.BorderColor3 = rgb(255,255,255)
	local dt = object:WaitForChild("Determination", 3) 
	dt.ImageTransparency = 1 
	dt.Parent = Gui.BlackFrame
end

local function MouseClicked(object)
	if TrueMenu.Value == false then
		while used == false do
			used = true
			
			local textbox = Gui:WaitForChild("Do not Be Silly, Kiddo."):Clone()
			textbox.Parent = Gui.BlackFrame
			Debris:AddItem(textbox,1.7)
			for i = 1, string.len(textbox.Text) do
				textbox.MaxVisibleGraphemes = i
				task.wait(0.05)
			end
			used = false
			return
		end
		return
	end

	while object.MaxVisibleGraphemes == 0 do wait() end
	if uses == maxuses then return end

	uses += 1
	print("Used in "..tostring(object).."! Have fun Player!")
	local sound1,sound2,sound3 = script.Text3,script.Text4,script.Text5

	if object.Name == "Play" then
		PlaySound(sound1)
	elseif object.Name == "Credits" then
		PlaySound(sound2)
	elseif object.Name == "Tutorial" then
		PlaySound(sound3)
	end

	tweens:Play()
	PlaySound(script.Text6)
	wait(6.8)
	Gui.Enabled = false
	wait(2)
	Gui.Parent:Destroy()
end


for _,v in pairs(Gui:GetDescendants()) do
	if v.ClassName == "TextButton" then
		v.MouseEnter:Connect(function() -- When mouse Enter Function
			MouseEnter(v)
		end)
		
		v.MouseLeave:Connect(function()
			MouseLeave(v)
		end)
		
		v.MouseButton1Click:Connect(function()
			MouseClicked(v)
		end)
	end
end

Hope I helped!

3 Likes

It helped organizating my code! Thank you :smiley:

2 Likes

All the pleasure is mine! Don’t forget to mark it as a solution if you want :wink:

4 Likes

Of course! Althought do you have any thoughts if I need to add a module to do that, the script?

2 Likes

No, it’s already optimized and working, it does not need anymore things.

4 Likes

No it’s not needed at all! The script would run the same

3 Likes

Sure! Thank you @griffeyiscool!

1 Like

Of course. Let me know if you need anymore help.

1 Like

What about those scripts? I made three TextButtons, and they will have a TweenService do make a animation on the Menu, like it appearing and stuff, how could I make that for the all three?

TextButton[1] script:

local TS = game:GetService("TweenService")
local tween = TweenInfo.new(5.4,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,8.4)
local textbox = script.Parent

local TP = TS:Create(textbox,tween,{Position = UDim2.new(0.144,0,0.3,0)})

TP:Play()
task.wait(8.8)
for i = 1, string.len(textbox.Text) do
	textbox.MaxVisibleGraphemes = i
	task.wait(0.05)
end

TextButton[2] script:

local TS = game:GetService("TweenService")
local tween = TweenInfo.new(5,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,12.8)
local textbox = script.Parent

local TP = TS:Create(textbox,tween,{Position = UDim2.new(0.856,0,0.3,0)})

TP:Play()
task.wait(13.5)
for i = 1, string.len(textbox.Text) do
	textbox.MaxVisibleGraphemes = i
	task.wait(0.05)
end

TextButton[3] script:

local TS = game:GetService("TweenService")
local tween = TweenInfo.new(1.5,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,25.3)
local textbox = script.Parent

local TP = TS:Create(textbox,tween,{Position = UDim2.new(0.5,0,0.195,0)})

TP:Play()

local TP2 = TS:Create(textbox,TweenInfo.new(0.999,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,false,0),{Position = UDim2.new(0.5,0,0.7,0)})

TP.Completed:Connect(function()
	TP2:Play()
		for i = 1, string.len(textbox.Text) do
			textbox.MaxVisibleGraphemes = i
			task.wait(0.05)
	end
	
end)

They’re almost all the same, how could I make that one script?

2 Likes

optimization wise, it’s best to use alternatives to waiting when you can

3 Likes

when creating tweens that will only be used once, just do this:

TS:Create(textbox,tween,{Position = UDim2.new(0.856,0,0.3,0)}):Play()
2 Likes

What do you mean? I didn’t understand!

1 Like

which reply did you not understand?

1 Like

“Optimization wise, it’s best to use alternatives to waiting when you can”, do you mean “task.wait()”?

1 Like

Also hi again @remcodesremcodes

2 Likes

yes, I am referring to the task.wait()s.
The problem with waiting is that it yields whole script

2 Likes

as this is a main menu (assuming there is no go back to menu button), it’d be better to use :Once() than :Connect

:Once is basically :Connect but calls :Disconnect() after it is fired, hence the name of the event

1 Like