How to store i,v values into a table?

Today, I wanted to know how to store a index and the value into a table by using table or any other way to save the players’ data into a table. I tried researching for any clues but I can’t find much information.

Example:

local Inventory = {
	['Item1'] = 1;
	['Item2'] = 2;
	['Item3'] = 3;
	['Item4'] = 4;
	['Item5'] = 5;
--- I wanted to add another item to the list.
}

Any help and feedback is appreciated! :slightly_smiling_face:

lets say you wanted to add that other item,

All you would have to do is

local Inventory = {}

Inventory['Wood'] = 1

--[[Or lets say you have a table of objects in a folder you wanted to loop through to insert this into the table]]

for i,v in pairs(tableThing) do
    Inventory[v] = i
end
1 Like

t[k] = v is the syntax to add a key to a dictionary

so
Inventory.Item6 = 6 or Inventory[“Item6”] = 6

1 Like

Hey @HahLua , while I was testing. I tried printing the table but the table is still empty.

local Inventory = {}

Inventory['Part'] = 1

for i,v in pairs(workspace.CoolObjects:GetChildren()) do
	Inventory[v] = i
	print(table.unpack(Inventory))
end
local Objects = {}

for i,v in pairs(game.Workspace.CoolObjects:GetChildren()) do
	Objects[v.Name] = i
end
print(Objects)
for i,v in pairs(Objects) do
	print(game.Workspace.CoolObjects[i])
end
1 Like