Get key name with key contents

Say this is a dictionary

local dictionary = {
["Apple"] = {
color = "red"
numbers = 5
}
["Orange"] = {
color = "red"
numbers = 5
}

}

If you have the orange dictionary, so if you do print(orange) it prints color and numbers, but how would you get the name if you have orange, for example , print(orange.Name) should print orange, but thats not the way to do it, so how do you print the name of it?

orange.Name isn’t an Instance though? :thinking: Bit confused on what you’re trying to accomplish here

If I have the dictionary orange as a variable
so if i do print(orange), it prints the color and numbers. How would I get that the dictionary name is “Orange”, since dictionary.Name does not exist, is there another way

you would replace [“orange”] with local orange

i cannot do that because orange is a key in the big dictionary, i meant like if i have the orange key and its contents, how would i get the key name.

[“key”] = {
value =5
}

How do i get that it is named “key” (I know I am quite bad at explaining things so if you need more tell me

you would do
local Orange = {
value = 5
}

Oh I see

Loop through the main Dictionary then, printing the keys & values

local dictionary = {
["Apple"] = {
color = "red"
numbers = 5
}
["Orange"] = {
color = "red"
numbers = 5
}

}

for Index, Value in pairs(dictionary) do
    print("Name of fruit: ", Index)
    print("Values of fruit: ", Value)
end

no but orange is a key in the big dictionary, so i cant make it a variable

So if dictionary parameter is orange, it will print “Name of fruit: Orange”?

just try this

local dictionary = {
local Apple = {
color = "red"
numbers = 5
}
local Orange = {
color = "red"
numbers = 5
}

}

Well, if you’d want to implement specific conditional checks if the Index’s name is Orange then yes

Otherwise it’d just print both fruits in the loop

Okay thank you I will try it. :smiley:

Ok so it did not work, I think it’d help if I dont show you the example and show you my actual thing.

RaceData = {
["RoadRace"] = {
PlayersEnrolledInRace = {}
RaceMax = 0
NumberOfRacers = 0,
}
}

This is my main dictionary, right now I am connecting to an onserverevent that returns the RoadRace dictionary inside the RaceData dictionary, I want to get the race name (which is “RoadRace”), how do I do that?

When I used your method, it printed index as 1 and value as NumberOfRacers

Ok found the issue

I believe this is because your [“RoadRace”] is encased in square brackets, if you remove those it should be referenced as a custom Index

Try this:

RaceData = {
RoadRace = {
PlayersEnrolledInRace = {}
RaceMax = 0
NumberOfRacers = 0,
}
}

for Index, Value in pairs(RaceData) do
    print("Name of fruit: ", Index)
    print("Values of fruit: ", Value)
end

image

this is how I added the race so how do i add it another way where it becomes the way you had it

You could just simply remove the square brackets that you see inside the races table, and it should reference them as variables

so do

Local name = Race.Name

if RaceData.name ~= nil then

and then to add it do RaceData.NewRace = {}?

I mean this

There’s a variable that’s called races in your script, which holds a table of all the info you want to search for

You don’t need to change anything inside your loop

races is a group in workspace that has all the races grouped so in races there is “RoadRace” group model
image

Maybe try replacing RaceData[race.Name] with table.find(RaceData, race.Name) instead?