I want to create a GUI for every index in a module script

  1. What do you want to achieve?
    I want to make a GUI for every index in a table using both the name of the index and the value.

  2. What is the issue?
    I can only return the value of an index and not the indexes name.

  3. What solutions have you tried so far?
    I have looked for solutions but came up short.

Is there any way you can get the name of the index in a table? Here is my table – it is in a module script. I need to get the name and the value so I can see what the player needs to create a certain item in the game. e.g. I can print “stone 5” but cannot get “Stone Wall”. I need to be able to get both.

local module = {
	--walls
	["Stone Wall"] = "stone 5";
	["Wood Wall"] = "wood 5";
	["Steel Wall"] = "steel 8";
	["Leaf Wall"] = "leaf 5";
	
	--detailed items
	["Desk"] = "wood 3";
	["Chair"] = "wood 2";
	["Light Source"] = "steel 2";
	["Bed"] = "wood 2 leaf 2";
	
	--Doors
	["Leaf Door"] = "leaf 3";
	["Wood Door"] = "wood 3";
	["Stone Door"] = "stone 3";
	["Steel Door"] = "steel 3";
}

your key is your key (big brain I know)

for example if your key is “Stone Wall”

if you did

print(module["Stone Wall"]) 

it would print “stone 5”

but if you did

print("Stone Wall") -- your key

it would print “Stone Wall”

Yes. What if I were to use a for loop to get all of the keys and their values, then assign the name of the GUI to the key and the GUI text to the value? It would not work.

You can do that, a for loop is written

for key, value in pairs()do
end

the key being the key and the value being the value

1 Like

I’ll try that right now. Thanks.

1 Like

It worked! I just never thought to print the key. I had it in mind only to print the value. Lol. I am dumb sometimes.

1 Like