Attempting to insert into reference table

I am attempting to write a function that runs once to compile a large collection of modules into a single reference table. The reason I need to do this is to get another piece of code to operate efficiently without looping through every single time.

I am able to get values from the looped modules, but whenever I try to insert them into my reference table, they refuse to go. I created a pcall to see if there’s some kind of formatting issue, and the pcall just errors out with table index is nil.

Build Function

mod.RegModuleReference = {}

mod.BuildModuleReference = function()
	
	for i,module in pairs(cm.reg:GetChildren()) do
		
		if module:IsA("ModuleScript") then
			local thisModule = require(module)
			print(module.Name.." "..thisModule["groupId"])
			print("\n")
			print("MODULE REFERENCE: Located module: "..module.Name)
			
			local success, errorReason = pcall(function()
				table.insert(
					mod.RegModuleReference,1,
					{
						[thisModule["groupId"]] = {
							1
						}
					}
				)
			end)
			
			if not success then
				error(errorReason)
			end
			
			print("MODULE REFERENCE: Inserted values from: "..module.Name)
			
			local testIndex = table.find(mod.RegModuleReference,"purplepeopleeater")
			print(testIndex)
			print(mod.RegModuleReference)
		end
		
	end
	
end

Edit 1: Error was resolved, but inserting into the reference table is still broken.

1 Like

testIndex probably doesn’t work because you are inserting a table that contains a dictionary into the table:

table.insert(
		mod.RegModuleReference, 1,
		{
			[thisModule["groupId"]] = {
				1
			}
		}
	)

Try looping through it and printing all of the values and the keys like this:

for key,value in pairs(mod.RegModuleReference) do
   print(key,value)
end
1 Like

This is correct somewhat. Inserting regular strings into the table also doesn’t work, so I don’t think it’s because of a dictionary.

1 Like

Is "groupId" part of every module you’re requiring?

try this:

mod.RegModuleReference[1] = {
   [thisModule["groupId"]] = {1}
}
1 Like

That doesn’t do the same thing

so the [thisModule["groupId"]] is probably not returning the string

1 Like

Yup, he probably doesn’t have "groupId" as part of every module script and it’s trying to index nil because it doesn’t exist

I do. The error has been fixed, but I still can’t insert anything into the table, even minor strings.

Is the code above still the one you’re using to insert items into the table? And the error is under the pcall?

Solution found. I typed out my table.insert incorrectly. Too many brackets.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.