Bad gui script needs improving

ok. I need the code to improve better game run-speed(performance). I made a long (month old) 86 lines of code I written.
I’m wondering what I could do to improve on 2 functions. How to make a better camera system? Write more table.insert to make it cleaner?

-- Variables --
local cam = workspace.CurrentCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SideButtons = script.Parent:WaitForChild("SideButtons")
local Menu = SideButtons:WaitForChild("Menu")
local MenuButtons = Menu:WaitForChild("MenuButtons")
local Play = MenuButtons:WaitForChild("Play")
local Donate = MenuButtons:WaitForChild("Donate")
local Donated = Menu:WaitForChild("Donated")
local Settings = MenuButtons:WaitForChild("Settings")
local MouseHover = script.Parent.Parent:WaitForChild("MouseHover")
local Dialog = require(ReplicatedStorage.Modules:WaitForChild("Dialog"))
local Player = game.Players.LocalPlayer
local tpService = game:GetService("TeleportService")
local Title = Menu:WaitForChild("Title")
local buttons = {}
local GuiManager = require(game.ReplicatedStorage.GameSettings:WaitForChild("GuiManager")) -- access to module script
 
local function positionCamera(pos) -- camera function
	 local target = pos
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CameraSubject = target
	cam.CoordinateFrame = CFrame.new(target.Position) * CFrame.Angles(0,11,0) -- cframe of part
end 

positionCamera(workspace:FindFirstChild("CameraPosition"))
GuiManager:SetMainMenu("Open")
GuiManager:SetNews("Open")
GuiManager:SetGuiEnabled(script.Parent.Parent:FindFirstChild("OpenButton"), false) -- openbutton, set to false if your on the menu screen
GuiManager:SetCore("ResetButtonCallback",false)
GuiManager:SetCore("TopbarEnabled",false)
game.ReplicatedFirst:RemoveDefaultLoadingScreen()


for _,button in ipairs(MenuButtons:GetChildren()) do
	if button:IsA("ImageButton") then
		table.insert(buttons, button)
	end
	
	for _,t in ipairs(buttons) do
			local hovered = t:FindFirstChild("Hover")
		t.MouseEnter:Connect(function()
			GuiManager:SetGuiEnabled(hovered,false)
				end)
				t.MouseLeave:Connect(function()
						GuiManager:SetGuiEnabled(hovered,false)
						end)
					end
end


local function playGame() -- play game function
	Dialog:ShowMessage("Sorry, this game is locked to developers only.")
	
		end



local function openMenu(enabled) -- quick function to open the menu
	GuiManager:SetGuiEnabled(MenuButtons, enabled)
end
openMenu(true) -- default. On play the menu will be visible


SideButtons.Set.Cancel.MouseButton1Click:Connect(function()
	openMenu(true)
	Title.Text = "Odyssey: Survival Enhanced"
	GuiManager:SetGuiEnabled(SideButtons.Set, false)
	end)

Settings.MouseButton1Click:Connect(function()
	openMenu(false)
	Title.Text = "Settings"
	GuiManager:SetGuiEnabled(SideButtons.Set,true)
	end)


MenuButtons.Donate.MouseButton1Click:Connect(function()
openMenu(false)
	Title.Text = "Donate your Robux"
	Donated.Position = UDim2.new(-0.025, 0,1.357, 0)
GuiManager:SetGuiEnabled(SideButtons.Don, true)
end)


Play.MouseButton1Click:Connect(playGame)
3 Likes

The code I was using is for a game helping with my friend TheGodlySpecter. I don’t really enjoy the UI I made but you may do:

gyazo.com/30e96fc6d418b66d4e84bacb8379832b

1 Like

From what I can see your algorithms are relatively simple and have been written well, I can’t see any improvements to be made on that front.
However, I would prefer to see more consistency in your coding style i.e. indentation, variable names (specifically capitalisation), and spacing. Remember that code is read more often than it is written. On that note however, your comments are used effectively and your code is very self-descriptive - I was able to understand its function without any further context.

All in all, solid code.

3 Likes

I’d suggest you don’t use MouseButton1Click for a gui that only needs to be clicked one, but use Activated. Creates better compatibility for mobile user especially.

1 Like
local cam = workspace.CurrentCamera
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SideButtons = script.Parent:WaitForChild("SideButtons")
local Menu = SideButtons:WaitForChild("Menu")
local MenuButtons = Menu:WaitForChild("MenuButtons")
local Play = MenuButtons:WaitForChild("Play")
local Donate = MenuButtons:WaitForChild("Donate")
local Donated = Menu:WaitForChild("Donated")
local Settings = MenuButtons:WaitForChild("Settings")
local MouseHover = script.Parent.Parent:WaitForChild("MouseHover")
local Dialog = require(ReplicatedStorage.Modules:WaitForChild("Dialog"))
local Player = game.Players.LocalPlayer
local GuiManager = require(game.ReplicatedStorage.GameSettings:WaitForChild("GuiManager"))

local buttons = {} -- Store buttons in a table

local function positionCamera(pos)
    cam.CameraType = Enum.CameraType.Scriptable
    cam.CameraSubject = pos
    cam.CoordinateFrame = CFrame.new(pos.Position) * CFrame.Angles(0, 11, 0)
end

local function setButtonHoverBehavior(button)
    local hovered = button:FindFirstChild("Hover")
    button.MouseEnter:Connect(function()
        GuiManager:SetGuiEnabled(hovered, false)
    end)
    button.MouseLeave:Connect(function()
        GuiManager:SetGuiEnabled(hovered, false)
    end)
end

-- Initialize button hover behavior
for _, button in ipairs(MenuButtons:GetChildren()) do
    if button:IsA("ImageButton") then
        table.insert(buttons, button)
        setButtonHoverBehavior(button)
    end
end

local function playGame()
    Dialog:ShowMessage("Sorry, this game is locked to developers only.")
end

local function openMenu(enabled)
    GuiManager:SetGuiEnabled(MenuButtons, enabled)
end

local function goToSettings()
    openMenu(false)
    Title.Text = "Settings"
    GuiManager:SetGuiEnabled(SideButtons.Set, true)
end

local function goToDonate()
    openMenu(false)
    Title.Text = "Donate your Robux"
    Donated.Position = UDim2.new(-0.025, 0, 1.357, 0)
    GuiManager:SetGuiEnabled(SideButtons.Don, true)
end

Play.MouseButton1Click:Connect(playGame)
Settings.MouseButton1Click:Connect(goToSettings)
Donate.MouseButton1Click:Connect(goToDonate)

-- Initial setup
positionCamera(workspace:FindFirstChild("CameraPosition"))
GuiManager:SetMainMenu("Open")
GuiManager:SetNews("Open")
GuiManager:SetGuiEnabled(script.Parent.Parent:FindFirstChild("OpenButton"), false)
GuiManager:SetCore("ResetButtonCallback", false)
GuiManager:SetCore("TopbarEnabled", false)
game.ReplicatedFirst:RemoveDefaultLoadingScreen()
openMenu(true)
Title.Text = "Odyssey: Survival Enhanced"

This code should be more organized, easier to read, and perform better by reducing redundant operations.

i dont think that needs to be edited you should focus on the gui appearance and the hover actions that makes it look better

I just don’t understand why you have so many variables

1 Like

Using descriptive variable names is another helpful practice to make your code more understandable. Rather than using abbreviations or short forms, opt for more precise and meaningful names that accurately describe the variable’s purpose. When defining functions, placing them at the top of your code is best so they can be declared before use. This ensures that your program runs smoothly and avoids any potential errors. Improving the camera positioning system is crucial for creating a smooth and user-friendly experience. Consider using a more straightforward method to set the camera’s position and orientation. Finally, it is essential to reduce redundancy in looping. Sometimes, nested loops may not be necessary and can slow down your program. Be mindful of your looping structures and try to simplify them whenever possible.

I have improved your code and am presenting you with a revised version.

-- Services & Modules
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GuiManager = require(game.ReplicatedStorage.GameSettings:WaitForChild("GuiManager"))
local Dialog = require(ReplicatedStorage.Modules:WaitForChild("Dialog"))

-- Variables
local cam = workspace.CurrentCamera
local SideButtons = script.Parent:WaitForChild("SideButtons")
local Menu = SideButtons:WaitForChild("Menu")
local MenuButtons = Menu:WaitForChild("MenuButtons")
local buttons = {}
local Player = game.Players.LocalPlayer

-- UI Elements
local Play = MenuButtons:WaitForChild("Play")
local Donate = MenuButtons:WaitForChild("Donate")
local Donated = Menu:WaitForChild("Donated")
local Settings = MenuButtons:WaitForChild("Settings")
local MouseHover = script.Parent.Parent:WaitForChild("MouseHover")
local Title = Menu:WaitForChild("Title")

-- Functions
local function positionCamera(pos)
	cam.CameraType = Enum.CameraType.Scriptable
	cam.CameraSubject = pos
	cam.CFrame = CFrame.new(pos.Position, pos.Position + Vector3.new(0, 0, 1))
end 

local function playGame()
	Dialog:ShowMessage("Sorry, this game is locked to developers only.")
end

local function openMenu(enabled)
	GuiManager:SetGuiEnabled(MenuButtons, enabled)
end

-- Setup
positionCamera(workspace:FindFirstChild("CameraPosition"))
GuiManager:SetMainMenu("Open")
GuiManager:SetNews("Open")
GuiManager:SetGuiEnabled(script.Parent.Parent:FindFirstChild("OpenButton"), false)
GuiManager:SetCore("ResetButtonCallback", false)
GuiManager:SetCore("TopbarEnabled", false)
game.ReplicatedFirst:RemoveDefaultLoadingScreen()
openMenu(true)  -- default menu visibility

-- UI Interaction
for _, button in ipairs(MenuButtons:GetChildren()) do
	if button:IsA("ImageButton") then
		table.insert(buttons, button)
		local hovered = button:FindFirstChild("Hover")
		button.MouseEnter:Connect(function()
			GuiManager:SetGuiEnabled(hovered, false)
		end)
		button.MouseLeave:Connect(function()
			GuiManager:SetGuiEnabled(hovered, false)
		end)
	end
end

SideButtons.Set.Cancel.MouseButton1Click:Connect(function()
	openMenu(true)
	Title.Text = "Odyssey: Survival Enhanced"
	GuiManager:SetGuiEnabled(SideButtons.Set, false)
end)

Settings.MouseButton1Click:Connect(function()
	openMenu(false)
	Title.Text = "Settings"
	GuiManager:SetGuiEnabled(SideButtons.Set, true)
end)

MenuButtons.Donate.MouseButton1Click:Connect(function()
	openMenu(false)
	Title.Text = "Donate your Robux"
	Donated.Position = UDim2.new(-0.025, 0, 1.357, 0)
	GuiManager:SetGuiEnabled(SideButtons.Don, true)
end)

Play.MouseButton1Click:Connect(playGame)

This post is from 2020 :sweat_smile:… OP must most definitely have improved the script by now…

1 Like

Fusion is your friend.