Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to print the name of a key in a dictionary?

Lets supose i have this:

local itens = {sword = 2, shield = 3}

and supose that I don't know the content of this table, so I want to print its names without its values, like this

sword
shield

how should I do it?

3 answers

✓ Accepted answer

Hey there!

The only method of getting the name of the key in the dictionary that I know of is using a for loop.

Example:

local items = {sword = 2, shield = 3}

for i,v in pairs(items) do
	print(i)
end

However, this will not print in order as dictionaries don't have an order. So the output will either be

--sword
--shield

or

--shield
--sword

#Do

print(itens.sword) -- 2

Apologies for lack of elaborate answer.

5 comments

0

Are you kidding me... (me dunno how to do it either lol)

0

ikr, and ALSO REMOVE THE MODERATION ON MY QUESTION >:( xD

0

why cant i take anything serious.

0

heey, i want it to print the key name, not the value inside it.

0

Oh, apologies I'm not sure how to do that, i think you need metatables to do that task then.

You can print by using itens[Numberhere] , for example:

print(itens[1])

It would print 2 (or shield idk)

1 comment

0

oh wait it print nil .........In order to use it u have to remove =1 and =2 then u can use if statement 

This discussion is preserved as it was. New answers, comments, and votes are closed.