Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Hooking Up Gui Class Module To Guis?

I'm trying to be really modular in the design of this game, I want to hook roblox gui instances to a class with methods, relating to gui appearance and display. But I'm unable to call what I believe is a callable method.

Local script in ReplicatedFirst:

print'loadhandler started'
--//Services
local _plrs = game:GetService'Players'
local _shared = game:GetService'ReplicatedStorage'
--//Constants
local _plr = _plrs.LocalPlayer
local _client = _plr:WaitForChild'PlayerScripts'
local _g = game
local _s = script
local _p = script.Parent
local _loaded = _s:WaitForChild'Loaded' --bindableevent in script
--//Modules
local clientModules = require(_client:WaitForChild'Modules') --a single module that holds required modules in it
--//Variables
local plrGui = _plr:WaitForChild'PlayerGui'


local mainGui = plrGui:WaitForChild'MainGui'
local screenFrame = mainGui:WaitForChild'ScreenFrame'
local loadingFrame = clientModules.GuiClass.new(screenFrame:WaitForChild'LoadingFrame')
local loadLabel = screenFrame.LoadingFrame:WaitForChild'LoadingLabel'
local skipButton = clientModules.GuiClass.new(loadLabel:WaitForChild'SkipButton')
local loadText = clientModules.GuiClass.new(loadLabel:WaitForChild'LoadText')

local loading = true
--//Code

loadingFrame:Open()
loadText:ChangeText'Loading Assets'

Module script in starterplayerscripts

local GuiClass = {}
local GuiMethods = {}


function GuiClass.new(Arg)

	return setmetatable({gui = Arg} or {gui = nil},GuiMethods)
	
	
end

function GuiMethods:Open(style)
	
	print'opening'	
	
	if style == nil then
		
		self.gui.Visible = true		
		
	end
	
	
end

function GuiMethods:Close(style)
	
	print'closing'	
	
	if style == nil then
		
		self.gui.Visible = false	
		
	end
	
	
end

function GuiMethods:ChangeText(text)
	
	self.gui.Text = text	
	
end

return GuiClass`

Error: ReplicatedFirst.LoadingHandler:29: attempt to call method 'Open' (a nil value) Stack Begin Script 'ReplicatedFirst.LoadingHandler', Line 29 Stack End

1 answer

Focusing mainly on the module the main problem you have is that your metatable isn't set quite right.

What you need to use is the __index metamethod. Say we have two tables:

local s = {dogs = 5}
local t = {cats = 12}

if we set the s table to the index metamethod for the t table then when we index the code will check if the index exists in t first, and then if it doesn't it will check s.

local s = {dogs = 5}
local t = setmetatable({cats = 12}, {__index = s})
print(t.cats)
print(t.dogs) -- dogs exists in s, not t! cool huh?

This is useful since it apply to methods too (functions). Note that when methods are called they have access to a self parameter. Thus if we use the index metamethod to let us call the method on the table you create in GuiClass.new then when those methods are actually used the self variable will be the unique table you created.

function GuiClass.new(Arg)
	return setmetatable({gui = Arg}, {__index = GuiMethods})
end

This discussion is preserved as it was. New answers, comments, and votes are closed.