 # How do i loop through this table

Not good with tables so

``````local tabled = {
A = 2,
B = 3,
C = 4,
D = 5,
E = 6
}
``````

Wondering how i could loop through a table like this in order
ipairs dont work and pairs just randomizes it somewhat

This is a dictionary. You could try something like this -

``````local List = {
["A"] = 1,
["B"] = 2,
["C"] = 6,

}

for p, q in pairs(List) do
print(p, q)
end
``````

BUT if you want to print their values in order, convert it into an array :

``````local tabled = {
{"C",2}, -- letter,value
{"B",5},
{"A",3},
}

--This will sort our table in the given order,
--in our case, we want to return it from smallest to largest
--a and b are the second values in each table in our array, which is the number/value.
table.sort(tabled,function(a,b)
return a < b
end)

for index,tab in ipairs(tabled) do
print(tab2])
--Would print 2,3,5 although it looks like in the original table it's supposed to be 2,5,3
end
``````

1 Like

You sadly can’t apply `table.sort` on dictionaries, nor on organized orders.
Only on arrays to my knowledge.

local tabled = {
[“A”] = 2,
[“B”] = 3,
…and so on
}

for k, v in pairs(tabled) do
print(k, v) —A, 2, B, 3, and so on
end

I’m on mobile, sorry for no text formatting.

Notice that if he wanted to print them in order [ their values], you can’t use this. `ipairs` won’t work in this code. So he would’ve needed to convert it into an array.

1 Like

As others have said you can’t conventionally get order from a looped dictionary. You might be able to make some kind of work around if you made an array with all the keys:

``````{"A","B","C"}
``````

If you looped through that it would get the ordered values and you could search for the correct values in the dictionary using each key.

1 Like

im pretty sure im interpreting this wrong but something like a normal table with keys matching the dictionary and i just pull from the dictionary with the normal table?

Correct, you would create a table for “the order of the keys” and then get the values by the key.

``````local KeysOrder = {"A", "B", "C"}
local Table = {A = 1, C = 3, B = 4}

for Index, Value in ipairs(KeysOrder) do
print(Table[Value])
end
``````

Output:

``````1
4
3
``````
1 Like

Alright i get it now, just a quick question so i dont have to make a entire new post
In one of my previous posts i was asking about a inventory system and they said to

“Simply store your frame in a table with the tool instance as the key”

Im not entire sure what to do here but heres what i think it means
Like when a child is added to the inventory create a new instance in the dictonary matching the tool as the thing and the frame as the other thing someting like

``````local D = {
Tool = Frame
}

-- Or

local B = {
Frame = Tool
}
``````

And it will just add a new one one on child added event

Not sure how to add a dictonary to a table
Not sure how to check for the instance when the child is removed

You’ll have to explain exactly what you’re trying to achieve.

In lua tables are dictionaries in a sense. You add a dictionary to an existing table by doing this:

``````local tab_ = {1,2,3}
tab_["NewValue"] = "new value"
``````

You’re going to have to explain what you mean.

basically when a child is added, it will a dictonary with the instance that got added, and a frame that was made from the instance, and when a child is removed, i want to check through the dictonary to find that instance that matches with the instances that is being removed, and remove the frame it created

Alright ill try this out

You can use bracket indexing to add a tool-frame pair to the dictionary, using the tool as the key. You can then find the frame and remove the tool-frame pair using the tool instance as the key once again.

``````local toolFrameMap = {}

local frame = -- create the frame

toolFrameMap[tool] = frame
end

local function onChildRemoved(tool)
local frame = toolFrameMap[tool]

-- destroy/clean up the frame

toolFrameMap[tool] = nil
end
``````
1 Like
``````local myTable = {
{"A", 2},
{"B", 3},
{"C", 4},
{"D", 5},
{"E", 6},
}

for i, data in ipairs(myTable) do
print(i, data, data)
end
``````

or

``````local instances = {}

local frame = Instance.new("Frame")

instances[child] = frame
end

local function ChildRemoved(child)
local frame = instances[child]
frame:Destroy()
instances[tool] = nil
end
``````

ayy it worked, tysm have been wondering how to do something like this for a bit  