Get Value from Index on dictionary

Hello, I have been trying to get the value of an index from a dictionary but I have some problems with it:

-- variables
local player = game.Players.LocalPlayer;
local character = player.Character or player.CharacterAdded:Wait();
local shop = script.Parent;
local container = shop.ScrollingFrame;
local temp = container.Temp;
-- services
local rs = game:GetService('RunService');
local rep = game:GetService('ReplicatedStorage')
-- tables
local backpacks = {};
local array = {};
-- code
for i, v in pairs(rep.Backpacks:GetChildren()) do
	backpacks[v.Name] = v:GetAttribute("Order")
end
for key, _ in pairs(backpacks) do
	table.insert(array, key)
end
table.sort(array);
for i, key in ipairs(array) do
	local backpack = backpacks[key];
	print(backpack); -- prints 1,2,3 witch are the orders of the backpacks
end;

But I am trying to print the name of the backpack not the index.
How do I do that?
Thank you!

2 Likes

Was the ipairs intended ? Maybe try for i,key in pairs(array) do.

I tried it earlier but sadly it did not work.

Could you print backpack and screenshot what it says?

1 Like

If you are trying to index an array it should be

for index, value in ipairs(array) do --An array
	local backpack = backpacks[index];
	print(backpack);
end;

Dictionaries would be:

for key, value in ipairs(dictionary) do --A dictionary
	local backpack = backpacks[key];
	print(backpack);
end;
1 Like

The “key” variable is already the name of the backpack. Just do:

for i, key in ipairs(array) do
	print(backpack); 
end;

printing(backpacks[key]) -- finds the value associated with the name of that backpack, which is why it is returning the index.

I tried it but here is what it prints:

Here is a screen shot of what it does:

Could you show me the children in the backpack?

Sorry, but do you mean this:

Hmmm on the line backpacks[v.Name] = v:GetAttribute(“Order”) you are indexing them by the child’s name?

I am trying to do something like this:

local backpacks = {
   ["A"] = 50

};

Ohh i see, you are actually indexing them using a string which holds another value in the table.

2 Likes

But how do I get the value of the index?

Are you actually able to use in pairs for the backpacks table since the indexes are not integers?

1 Like

The Array holds the actual indexes for backpack which are the names but you are printing the values of these indexes.

My solution is do local backpack = key and im sure it will print the name.