C stack overflow error

I’m currently trying to create a table using a metatable however I keep running into the error “C stack overflow”, here is my code:

local FlagMetatable = {
	__index = function(Table, key)
		
		warn(key.." does not have a table, creating one.")
		Table[key .. "-flags"] = {1}
		
		for i,v in pairs(Table[key]) do
			print(i)
			print(v)
		end
		
		warn(Table[key])
		
		table.insert(Table[key], 1)
		
		return Table[key]
		
	end
}

The script calling it:

				for i,v in pairs(FlagTable[Player.UserId]) do
					if v then
						v = v + 1
						print(FlagTable[Player.UserId])
					else
						print(FlagTable[Player.UserId])
					end
				end

I would like the structure of the table to look like this:

local FlagTable = {
     ["userid"] = {
       1
  }
}

Any help is appreciated, thanks

This will create an infinite recursion since it will fire the __index metamethod again. Use the rawget function to get the value of an index within a table without triggering the __index method.

1 Like

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