How to get the name of a value in a dictionary?

hello im trying to get the name of a value in a dictonary.

local things = {
	["1"] = "RandomThing",
	["2"] = "Rwetpw",
	["3"] = "rwerwe",
	["4"] = "ewfef"
}

for i,v in pairs(things) do
	-- get name of dictonary value (1-4)
	-- I dont know how to get name so for example ill use v.Name
	if tonumber(v.Name) > 2 then -- THIS DOESN'T WORK... HOW WOULD I DO THIS?
		print("success")
	end
end

You can just do this:

local things = {
      "RandingThing",
      "AndSoOn"
}

The “v” value is now your string, and the “i” value is default. There’s another fix, and that’s your “v.Name.” Just put “i,” since it’s a table and not unless there’s a table inside of “i” with an object called “Name,” then this will obviously error.

I do recommend you to not put the index values in the tables like that, since they’re default, and it’ll just be a few clicks do fix it anyway.

local things = {
	{"1","RandomThing"},
	{"2","Rwetpw"},
	{"3","rwerwe"},
	{"4","ewfef"}
}

for i,v in pairs(things) do
	if tonumber(v[1]) > 2 then 
		print("success")
	end
end

I’m not doing this because the dictonary’s value is not useless in my situation… I’m just using these practical examples

1 Like

I dont think this is how dictionaries work…

i is the name of the value in the dictionary (index, value)

Well then forget the code shortening I guess, since that’s not what you want. Just don’t do v.Name, do i

Edit: man I love when ppl speedrun to get a solution for these easy posts

local things = {
	["1"] = "RandomThing",
	["2"] = "Rwetpw",
	["3"] = "rwerwe",
	["4"] = "ewfef"
}

for key, value in pairs(things) do
	if tonumber(key) > 2 then
		print("success", key)
	end
end

Just use a function to compare the requested name from what’s in the dictionary

function GetValueByName(Name: string)
   for _, val in pairs(things) do
      return val == Name and val or continue
   end
end
1 Like

I is the number of times it has looped through in the loop not the key. Dictionary looping is random so it wont work

1 Like

That’s just tedious if you aren’t making a giant script comparing tens of tens of values.

I swear it isn’t random…

Edit: plus, the “i” values sticks to the “v” value so you got this all wrong bruh

It is tedious, because its not a simple array you can just table.find() . It’s a dictionary and that’s the most simple function that gets values from non numerical indexes.

1 Like

So, let me explain:

i               v
number    string in your case

these two values stick together, and not unless you’re using ipairs to loop through it, then it isn’t random.

local dictionary = {
["Apple"] = 5
["Banna"] = 5
}

for key,v in pairs(dictionary) do
print(key)
end

expected output should be:

Apple 
Banna

right?

1 Like

yes

extras extra extras cmonnnn

I dont know who to give the solution to

1 Like

I have the above post that explains it

This isn’t a numerical loop:

for i = x, y do
  --...
end

i in this instance is the key:


1 Like

no dude, putting numbers at the starts is useless since the “i” is default and is basically doing that for you

Edit: plsplsplsplspls don’t solution yourself