This isn’t really about structuring you UI objects and script locations. I’m more stuck in the part where every time I script user interface the code looks extremely messy. Although it works and there’s no issue with it; it’s hard to read. I would appreciate any tips to organise it more.
A sample of ui code from my game:
local CONTENT_PROVIDER = game:GetService("ContentProvider")
local REPLICATED_FIRST = game:GetService("ReplicatedFirst")
local REPLICATED_STORAGE = game:GetService("ReplicatedStorage")
local TWEEN_SERVICE = game:GetService("TweenService")
local WORKSPACE = game:GetService("Workspace")
local PLAYERS = game:GetService("Players")
local RUN_SERVICE = game:GetService("RunService")
local GET_ASSETS = require(script.GetAssets)
local Player = PLAYERS.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = WORKSPACE.CurrentCamera
local Cameras = WORKSPACE:WaitForChild("Cameras")
local Sounds = script:WaitForChild("Sound")
local BorderPosts = WORKSPACE:WaitForChild("BorderPosts")
-- replicating objects
local RepObjects = REPLICATED_STORAGE:WaitForChild("ReplicatedObjects")
for _, v in pairs(RepObjects:GetDescendants()) do
local newObject = v:Clone()
if v.Parent.Name == "Cameras" then
newObject.Parent = Cameras
elseif v.Parent.Name == "SpectateCameras" then
newObject.Parent = BorderPosts[v.Name]
newObject.Name = "SpectateCamera"
elseif v.Parent.Name == "BorderTeleports" then
newObject.Parent = BorderPosts[v.Name]
newObject.Name = "TeleportPlayerPart"
end
end
local MOVE_SPEED = 150
local MainMenuCam = Cameras:WaitForChild("MainMenuCamera")
function UpdateCamera()
local Center = MainMenuCam.CFrame
local MoveVector = Vector3.new((Mouse.X - Center.X)/MOVE_SPEED, (Mouse.Y - Center.Y)/MOVE_SPEED, 0)
Camera.CFrame = MainMenuCam.CFrame * CFrame.Angles(math.rad(-(Mouse.Y - Center.Y)/MOVE_SPEED), math.rad(-(Mouse.X - Center.Y)/MOVE_SPEED), 0)
end
-- GUI
local PlayerGui = Player:WaitForChild("PlayerGui")
local MainMenu = PlayerGui:WaitForChild("MainMenu")
local LoadingScreen = MainMenu.LoadingScreen
local Menu = MainMenu.Menu
local BorderSelection = MainMenu.BorderSelection
REPLICATED_FIRST:RemoveDefaultLoadingScreen()
local Assets = GET_ASSETS.FetchAssets()
local Loading = true
for i = 1, #Assets do
local Asset = Assets[i]
CONTENT_PROVIDER:PreloadAsync({Asset})
if i % 5 == 0 then
task.wait()
end
TWEEN_SERVICE:Create(LoadingScreen.FillBackground.Fill, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {Size = UDim2.new(i / #Assets, 0, 1, 0)}):Play()
if i == #Assets then
LoadingScreen.FillBackground.LoadingLabel.Text = "Complete"
Loading = false
end
end
local Connection = RUN_SERVICE.RenderStepped:Connect(UpdateCamera)
Player:RequestStreamAroundAsync(MainMenuCam.Position)
task.wait(1)
LoadingScreen.FillBackground:TweenPosition(UDim2.new(0.5, 0,1.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
task.wait(1)
TWEEN_SERVICE:Create(LoadingScreen.Title, TweenInfo.new(2), {TextTransparency = 1}):Play()
task.wait(2.5)
TWEEN_SERVICE:Create(LoadingScreen, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
task.wait(0.6)
Menu:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
function Transition(time)
TWEEN_SERVICE:Create(LoadingScreen, TweenInfo.new(0.5), {BackgroundTransparency = 0}):Play()
task.wait(time)
TWEEN_SERVICE:Create(LoadingScreen, TweenInfo.new(0.5), {BackgroundTransparency = 1}):Play()
end
for _, object in pairs(Menu:GetChildren()) do
if object:IsA("TextButton") then
object.MouseEnter:Connect(function()
TWEEN_SERVICE:Create(object, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(54, 54, 54)}):Play()
object:TweenSize(UDim2.new(0.352, 0,0.103, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
Sounds.Hover:Play()
end)
object.MouseLeave:Connect(function()
TWEEN_SERVICE:Create(object, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(62, 62, 62)}):Play()
object:TweenSize(UDim2.new(0.345, 0,0.087, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
end)
object.MouseButton1Click:Connect(function()
Sounds.Click:Play()
if object.Name == "PlayButton" then
Menu:TweenPosition(UDim2.new(0.5, 0,1.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
task.wait(0.3)
Transition(0.5)
Connection:Disconnect()
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = BorderPosts["1"].SpectateCamera.CFrame
BorderSelection:TweenPosition(UDim2.new(0.5, 0,0.749, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
end
end)
end
end
local CurrentCameraIndex = 1
BorderSelection.SelectButton.MouseButton1Click:Connect(function()
BorderSelection:TweenPosition(UDim2.new(0.5, 0,1.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
task.wait(0.5)
Transition(1)
Connection:Disconnect()
Camera.CameraType = Enum.CameraType.Custom
Player:RequestStreamAroundAsync(BorderPosts[tostring(CurrentCameraIndex)].TeleportPlayerPart.Position)
Player.Character.HumanoidRootPart.CFrame = BorderPosts[tostring(CurrentCameraIndex)].TeleportPlayerPart.CFrame
task.wait(1)
CurrentCameraIndex = 1
end)
BorderSelection.SelectButton.MouseEnter:Connect(function()
TWEEN_SERVICE:Create(BorderSelection.SelectButton, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(61, 147, 43)}):Play()
BorderSelection.SelectButton:TweenSize(UDim2.new(0.45, 0,0.207, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
Sounds.Hover:Play()
end)
BorderSelection.SelectButton.MouseLeave:Connect(function()
TWEEN_SERVICE:Create(BorderSelection.SelectButton, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(75, 186, 55)}):Play()
BorderSelection.SelectButton:TweenSize(UDim2.new(0.426, 0,0.173, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
end)
BorderSelection.ReturnButton.MouseButton1Click:Connect(function()
CurrentCameraIndex = 1
BorderSelection:TweenPosition(UDim2.new(0.5, 0,1.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
task.wait(0.5)
Transition()
Menu:TweenPosition(UDim2.new(0.5, 0,0.5, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.5)
Connection = RUN_SERVICE.RenderStepped:Connect(UpdateCamera)
Player:RequestStreamAroundAsync(MainMenuCam.Position)
end)
BorderSelection.ReturnButton.MouseEnter:Connect(function()
TWEEN_SERVICE:Create(BorderSelection.ReturnButton, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(147, 2, 4)}):Play()
BorderSelection.ReturnButton:TweenSize(UDim2.new(0.35, 0,0.119, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
Sounds.Hover:Play()
end)
BorderSelection.ReturnButton.MouseLeave:Connect(function()
TWEEN_SERVICE:Create(BorderSelection.ReturnButton, TweenInfo.new(0.1, Enum.EasingStyle.Sine), {BackgroundColor3 = Color3.fromRGB(186, 2, 5)}):Play()
BorderSelection.ReturnButton:TweenSize(UDim2.new(0.343, 0,0.107, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Sine, 0.1)
end)