I have a really simple issue that's been keeping me riled the past hour pertaining to looping through two tables and matching their keys up.
On one table, it's getting the children in a folder (They are 6 IntValues but I really want their Name property), and on the other table, its a dictionary of 6 keys with their values (Those values are intergers). What I need it to do is match up the IntValues' names from the first table with the keys on the second table. The two tables are seperated into a Script and ModuleScript but that shouldn't matter.
####Script - Where I want to match up the table
--This is a function that fires from a RemoteEvent
local function onSentChosenClass(plr, chosenClass)
if chosenClass then
for i, v in pairs(PlayerStatsFolder:FindFirstChild("Attributes"):GetChildren()) do
--Trying to debug the Metatable thats in the ModuleScript. Prints nil. How dandy.
print(ModuleScript.AttributeMetatable(chosenClass)[v.Name])
--[[ Previous attempt before the use of _index thats commented out but prints the values of the 2nd table. Ignore the if statement, its always false.
if v.Name == ModuleScript.Classes[chosenClass].classAtt[v.Name] then
print(ModuleScript.Classes[chosenClass].classAtt[v.Name])
end
]]
end
end
end
####ModuleScript - Holds the dictionary and a function that returns the metatable
local settings = {}
--Literally copied/pasted from Wiki
local returnMetatable = {
_index = function(self, index)
return index
end
}
--This returns the table but I don't seem to be getting the right key/attribute
function settings.AttributeMetatable(className)
setmetatable(settings.Classes[className].classAtt, returnMetatable)
return returnMetatable
end
settings.Classes = {
Warrior = {
classAtt = {
-- I want the names of the keys (STR, CON, etc)
STR = 7,
CON = 5,
DEF = 6,
DEX = 4,
INT = 4,
LUC = 4
}
}
}
return settings
What I have written down and also have reworked alot is using a metatable and _index to return the key. I got this off Roblox Wiki's metamethods but these are so hard to understand for a below average scripter like my self.
After matching the second table's keys with the names of the IntValues' Name property then its simply setting the IntValues' value to the 2nd table's values. Freaking boring and confusing explanation, right?
I don't expect a rework made to my code, yet that with an explanation is all the more plentiful. Thanks.

1 comment
Much thanks but I had it figured out how to solve this during college class today and, funny enough, its identical to your answer. Also, the plan was to structure this using OOP but I'm just messing around for now. Thanks again.