#How can i optimize this code?`

25 messages · Page 1 of 1 (latest)

dim ivy
#

I wanted an example on how i can use metatables for this

#

i think they might work with this

noble swift
# dim ivy

How can you use metatable thingies on this

plush lichen
#

Just lemme breakfast

#

But rn you can un-nest the main code

local function PopupFunc(...)
  if LastPopup ~= Title then
    ...
  end
end

Into

local function PopupFunc(...)
  if LastPopup == Title then return end
  ...
end
plush lichen
# dim ivy

ok lets imagine ( cause i dont have your architecture ) that you have a PopupClone frame that contains Buttons folder with textbuttons in it

#

so i have something that look like this

#

so now that i have the architecture, in my Popup module ill create a class

#
local Class = {}

local Popup = {}
local __MetaPopup = {__index = Popup}

function Class.new()
    local self = setmetatable({}, __MetaPopup)
    return self
end

return Class

#

now when i do

#
local Popup = require(script.Popup)

local popup = Popup.new()

it will create me a new popup

#
local Class = {}

local Popup = {}
local __MetaPopup = {__index = Popup}

local popupPattern = assert(script.Parent:FindFirstChild("Popup"))

function Popup:Destroy()
    
end

function Popup:Result1()
    
end

function Popup:Result2()

end

function Class.new(title, option1, option2)
    if Class.LastPopup then return end
    local self = setmetatable({}, __MetaPopup)
    self.Instance = popupPattern:Clone()
    return self
end

return Class

#

now that i have this, my popup created can have the destory and result1 methods

#
local Class = {}

local Popup = {}
local __MetaPopup = {__index = Popup}

local popupPattern = assert(script.Parent:FindFirstChild("Frame"))
local mainFrame = assert(script.Parent.Parent:FindFirstChild("MainFrame"))

function Popup:Destroy()
    Class.LastPopup = nil
    self.Instance:Destroy()
end

function Popup:Result1()
    -- Do something
end

function Popup:Result2()
    -- Do something
end

function Class.new(title, option1, option2)
    if Class.LastPopup then return end
    local self = setmetatable({}, __MetaPopup)
    self.Instance = popupPattern:Clone()
    Class.LastPopup = self
    self.Instance.Parent = mainFrame
    
    -- Connect buttons
    local buttons = assert(self.Instance:FindFirstChild("Buttons"))
    local op1 = assert(buttons:FindFirstChild("Option1"))
    local op2 = assert(buttons:FindFirstChild("Option2"))
    op1.Text = option1
    op1.MouseButton1Down:Connect(function() self:Result1() end)
    op2.Text = option2
    op2.MouseButton1Down:Connect(function() self:Result2() end)
    
    -- Connect close
    local close = assert(self.Instance:FindFirstChild("Close"))
    close.MouseButton1Down:Connect(function() self:Destroy() end)
    return self
end

return Class

#
local Pop = require(script.Popup)
local popup = Pop.new("Hello", "foo", "bar")
#

in my main

#

if you wanna add infinte amount of popup, you can remove all of the "LastPopup" lines

#

cause if you just want 1 popup, you dont really need all this class thingy

#

you can remove all the Popup things and just duplicate as you did

#

but if you want an infinite amount, this is a better approach

dim ivy
#

Woah thanks

#

this is extremely well explained