The code below works fine, the issue I have with it is that specifically
["General3"] = {
["Sub1"] = {
["G3-S1-Value1"] = {
"Cat1",
"Cat2",
"Cat3"
},
"G3-S1-Value2",
"G3-S1-Value3",
},
Is not ordered, I am aware this is due to the fact that [“G3-S1-Value1”] isn’t a string however I do not know how to fix it, help is appreciated
Full code:
local UIS = game:GetService("UserInputService")
local masterTable = {
["General1"] = {
["Sub1"] = {
"G1-S1-Value1",
"G1-S1-Value2",
"G1-S1-Value3",
},
["Sub2"] = {
"G1-S2-Value1",
"G1-S2-Value2",
"G1-S2-Value3",
},
["Sub3"] = {
"G1-S3-Value1",
"G1-S3-Value2",
"G1-S3-Value3",
},
},
["General2"] = {
["Sub1"] = {
"G2-S1-Value1",
"G2-S1-Value2",
"G2-S1-Value3",
},
["Sub2"] = {
"G2-S2-Value1",
"G2-S2-Value2",
"G2-S2-Value3",
},
["Sub3"] = {
"G2-S3-Value1",
"G2-S3-Value2",
"G2-S3-Value3",
},
},
["General3"] = {
["Sub1"] = {
["G3-S1-Value1"] = {
"Cat1",
"Cat2",
"Cat3"
},
"G3-S1-Value2",
"G3-S1-Value3",
},
["Sub2"] = {
"G3-S2-Value1",
"G3-S2-Value2",
"G3-S2-Value3",
},
["Sub3"] = {
"G3-S3-Value1",
"G3-S3-Value2",
"G3-S3-Value3",
},
},
}
local masterOrder = {
"General1", "General2", "General3"
}
local subOrder = {
"Sub1", "Sub2", "Sub3"
}
local CatOrder = {
"Cat1", "Cat2", "Cat3"
}
local currentTable = masterTable
local tableHistory = {}
local Visible = false
function getOrderedKeys(tbl, orderReference)
local orderedKeys = {}
for key, _ in pairs(tbl) do
table.insert(orderedKeys, key)
end
table.sort(orderedKeys, function(a, b)
local indexA = table.find(orderReference, a) or math.huge
local indexB = table.find(orderReference, b) or math.huge
return indexA < indexB
end)
return orderedKeys
end
function assignButtonTexts(desiredTable)
local index = 1
if typeof(desiredTable) ~= "string" then
local orderReference = masterOrder
if currentTable ~= masterTable then
orderReference = subOrder
end
if currentTable == masterTable["General3"]["Sub1"]["G3-S1-Value1"] then
orderReference = CatOrder
end
local orderedKeys = getOrderedKeys(desiredTable, orderReference)
for _, key in ipairs(orderedKeys) do
local value = desiredTable[key]
if type(value) == "table" then
script.Parent:FindFirstChild(index).Text = key
else
script.Parent:FindFirstChild(index).Text = value
end
index += 1
end
else
print(desiredTable)
currentTable = masterTable
table.clear(tableHistory)
assignButtonTexts(currentTable)
end
end
assignButtonTexts(currentTable)
local validKeys = {Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three}
local ascociatedNumbers = {["Enum.KeyCode.One"] = 1, ["Enum.KeyCode.Two"] = 2, ["Enum.KeyCode.Three"] = 3}
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.T then
Visible = not Visible
script.Parent.Enabled = Visible
if not Visible then
currentTable = masterTable
table.clear(tableHistory)
assignButtonTexts(currentTable)
end
end
if input.KeyCode == Enum.KeyCode.Backspace and #tableHistory > 0 then
currentTable = table.remove(tableHistory)
task.wait(0.1)
assignButtonTexts(currentTable)
end
if not table.find(validKeys, input.KeyCode) then return end
local key = ascociatedNumbers[tostring(input.KeyCode)]
table.insert(tableHistory, currentTable)
local orderReference = masterOrder
if currentTable ~= masterTable then
orderReference = subOrder
end
local orderedKeys = getOrderedKeys(currentTable, orderReference)
local selectedKey = orderedKeys[key]
if selectedKey then
currentTable = currentTable[selectedKey]
task.wait(0.1)
assignButtonTexts(currentTable)
end
end)