How can I make opening a Gui with a ModuleScript easier?

Hello!
So, I’m currently working with module scripts with guis for the first time! I’ve provided my code below and I’ll explain more about it so you can understand what I’m trying to accomplish. If I can do anything to improve my use with Module Scripts or you have any tips that I could benefit from please let me know!

Module Script


local module = {}
	local findframe = script.Parent.Parent.Gui -- Finding the Gui
	local closeframe = script.Parent -- Getting all of the frames in my gui
	local Current = script.Parent.CurrentGui.Value -- Ignore I couldn't get this to work properly

	module["Click"] = function()  -- function for clicking a button
		script.Sounds.Click:Play() -- Plays sound
	end
	
	module["Open"] = function(frame) -- function for opening the targetted gui
		local frame = findframe:FindFirstChild(frame):TweenPosition(UDim2.new(0.5,0,0.5,0), "In","Bounce",1) -- tween the gui
		Current = tostring(frame)  -- frame is the name of the frame I want to open
	end
	
	module["Close"] = function(frame) -- Pretty self explanatory
		closeframe:FindFirstChild(frame):TweenPosition(UDim2.new(0.5,0,1.5,0), "Out","Bounce",1)
	end

return module

This is a local script handling guis


local module = require(script.Parent:WaitForChild('GuiModule'))
local player = game.Players.LocalPlayer
local gui = script.Parent.Parent.Gui
local test = "test" -- I'd have to do variables for every gui in my game

-- Open And Closing GUI's
gui.Open.MouseButton1Click:Connect(function()
	module["Click"]() -- Plays a Click sounds
	module["Open"](test) -- Opens a gui with the name "test"
end)

gui.test.Close.MouseButton1Click:Connect(function()
	module["Close"](test) -- Closes the gui
	module["Click"]() -- yet again, plays the sound
end)

Thank you for reading and helping me if you can. If you have any questions please comment I’ll do my best to respond as soon as I can! Have a great day!

3 Likes
-- MODULESCRIPT
function module:Click()-- function for clicking a button
	script.Sounds.Click:Play() -- Plays sound
end

-- LOCALSCRIPT
module:Click()

Simplify your code where possible, it makes it easier to understand. Only complicate things if it’s necessary and in your case, it is not.

Ok, I’ve rewritten your code to be cleaner and more efficient.


local module = {}
	local findframe = script.Parent.Parent.Gui -- Finding the Gui
	local closeframe = script.Parent -- Getting all of the frames in my gui
	local Current = script.Parent.CurrentGui.Value -- Ignore I couldn't get this to work properly
	
	function module:Open(frame) -- function for opening the targetted gui
		script.Sounds.Click:Play()
		local frame = findframe:FindFirstChild(frame):TweenPosition(UDim2.new(0.5,0,0.5,0), "In","Bounce",1) -- tween the gui
		Current = tostring(frame)  -- frame is the name of the frame I want to open
	end
	
	function module:Close(frame) -- Pretty self explanatory
		script.Sounds.Click:Play()
		closeframe:FindFirstChild(frame):TweenPosition(UDim2.new(0.5,0,1.5,0), "Out","Bounce",1)
	end

return module

local module = require(script.Parent:WaitForChild('GuiModule'))
local player = game.Players.LocalPlayer
local gui = script.Parent.Parent.Gui
local test = "test" -- I'd have to do variables for every gui in my game

-- Open And Closing GUI's
gui.Open.MouseButton1Click:Connect(function()
	module:Open(test) -- Opens a gui with the name "test"
end)

gui.test.Close.MouseButton1Click:Connect(function()
	module:Close(test) -- Closes the gui
end)
6 Likes

So, thank you! It worked! But, am I able to have a different way of finding the frame? The frame in this example, "local test = “test” so that’s the frame that will open when the button is clicked. How can I not make a variable for each frame in my gui?

You could iterate through a list of frames you want to open and call the module:Open(frame) function for each one.

1 Like