Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Needing help with printing a string from a table in a table (?)

Asked by 1 year ago
local example = {
    ["Example"] = {
        ["ex1"] = "a",
        ["ex2"] = "b",
        ["ex3"] = "c",
        ["ex4"] = "d",
    }
}

for _,btns in pairs(script.Parent:GetChildren()) do
    if btns:IsA("ImageButton") then
        btns.MouseButton1Click:Connect(function()
            if table.find(example ,btns.Name) then --stops working here
                local info = table.find(example ,btns.Name)

                print(info.1) 
            end
        end)
    end
end

2 answers

Log in to vote
0
Answered by
04nv 0
1 year ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an answer.

use the key, not the index.

print(info.1) -- which should actually be info[1]

should be

print(info["ex1"])
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
1 year ago
local example = {
    ["Example"] = {
        ["ex1"] = "a",
        ["ex2"] = "b",
        ["ex3"] = "c",
        ["ex4"] = "d",
    }
}

Tables are split into two components: the array component, and the hash-table (dictionary) component. Arrays are defined by ascending numerical indices.

table.find will perform a search through the array component by ascending through its indices, however, as example is entirely a dictionary, that search will quickly cease. The key-value pairs you desire are also not directly within example.

The correct code is:

local info = example.Example[btn.Name]
if info then
    -- Use 'info' ...
end

The information at example.Example[btn.Name] is also a string, so writing info.1 does not make sense. The syntax itself is illegal.

It seems like you need to read up on tables.

Answer this question