I am writing framework which would require me to index tables to acquire a value within them, but I cannot seem to index a table which is holding other tables.
Hey everyone! I’m currently in the midst of programming the systems for an upcoming project of mine, and one of the necessities will be accessing tables stored by modulescripts.
However, upon implementation of this, I realized that indexing would be… a little tedious.
Here’s an example of my problem:
-- MODULE
return function()
local infoMain = {
-- data would go here
["TBL"] = {
Id = 1;
};
};
return infoMain
end
-- acquisition:
local returnedTBL = require(module)()
print(returnedTBL[1].Id) -- prints nil
print(returnedTBL[1]) -- also prints nil
If anybody with more programming experience than me can chime in or provide support, it’d be greatly appreciated.
The issue with this is… each table within the returned data will have a different name, so in order to change the actual index to not be an integer, I would have to access the name of the actual index itself through a labyrinth of Serverside architecture… however, I’ll wait for other people’s inputs.
Ah, if there are multiple keys, would maybe a for-loop work for you? It would loop over the table and give you the values in pairs of the key (ability name), and table (ability data)
local abilities = {
Punch = { Id = 23 },
Fire = { Id = 45 }
}
for abilityName, abilityInfo in abilities do
print(abilityName, abilityInfo) -- Would return for each ability in the list
print(abilityInfo.Id)
end
Yes, it does reutrn infoMain as the returned value. However, I intialized infoMain.Attacks = Abilities, which should’ve set .Attacks to be the table holding all the abilities.
“Edit: Also, why are you calling the return value of require(module)?” - I’m sorry I don’t quite get what you mean. Do you mean why I’m returning a function or?
Okay, I just reiterated through my code and realized it was a mistake on my end. I’m gonna mark your iterator statement as the solution. Thanks for your time though, I really appreciate it.