I am trying to make a “directory” system for a table. I have seen it done before but I do not know how to do it myself or how it works. In short, I am trying to achieve something like this:
local Values = {}
function NewButton(Section, Called, Txt)
local factor = 8
local TextButton = Create("TextButton", {
BackgroundTransparency = 1,
Name = Called,
Parent = Section,
Size = UDim2.new(0, factor, 0, factor),
Visible = true,
ZIndex = 1,
Text = Txt
})
-- I have this
local SubArray = {"NewButton", Section, Called, TextButton}
table.insert(Values, SubArray)
end
NewButton(ScreenGui, "Example button", "Click me")
-- What I am trying to do
print(Values["NewButton"][ScreenGui]["Example button"].Text)
-- I want the above line to print the Text on the TextButton that NewButton created
I considered doing something like this:
for i, v in pairs(Values) do
if v[1] == "NewButtom" then
if v[2] == ScreenGui then
if v[3] == "Example Button" then
print(v[4].Text)
end
end
end
end
But I am worried that this will be unusable for large tables as it will be very slow as the function iterates over the large table.
I am looking for an answer that goes somewhat like this:
local Values = {}
function NewButton(Section, Called, Txt)
local factor = 8
local TextButton = Create("TextButton", {
BackgroundTransparency = 1,
Name = Called,
Parent = Section,
Size = UDim2.new(0, factor, 0, factor),
Visible = true,
ZIndex = 1,
Text = Txt
})
-- Table stuff here to make print(Values["NewButton"][ScreenGui]["Example button"].Text) return Txt
end
NewButton(ScreenGui, "Example button", "Click me")
print(Values["NewButton"][ScreenGui]["Example button"].Text) -- "Click me"