Loop in gettable

What does loop in gettable mean? Because I get this in my script

-- Module
local  Embed = {}
Embed.__index = Embed
Embed.__call = function(t, ...) return Embed end
Embed.ClassName = "Embed"

function Embed.new(data)
	local self = {}
        self.__index = self

	self.rawdata = {}
	
	return setmetatable(self, Embed)
end

function Embed:addField(name, value, inline)
	_ScanParam({"name", name}, {"string"}, true, true, {1, 256})
	_ScanParam({"value", value}, {"string"}, true, true, {1, 1024})
	_ScanParam({"inline", inline}, {"boolean"}, false, true)
	if not (self.rawdata.fields) then
		self.rawdata.fields = {}
	end
	local field = {}

	field.name = name
	field.value = value
	field.inline = inline

	table.insert(self.rawdata.fields, field)

	return self
end

function Embed:addFields(...)
	local fields = {...}
	for _, field in ipairs(fields) do
		_ScanParam({"field", field}, {"table"}, false, true)
		Embed:addField(field.name, field.value, field.inline)
	end

	return self
end

return setmetatable(Embed, Embed)

-- Script
local new = mod.new()
:addField("Name", "Value", true)
:addFields(
	{name = "Name", value = "Value"},
	{name = "Name", value = "Value", inline = true}
);

But if I make like this

local Embed = {}
Embed.__index = Embed
Embed.__call = function(t, ...) return Embed end
Embed.ClassName = "Embed"

function Embed.new(data)
	local self = {}
	self.__index = self
	
	self.rawdata = {}
	
	function self.addField(name, value, inline)
		_ScanParam({"name", name}, {"string"}, true, true, {1, 256})
		_ScanParam({"value", value}, {"string"}, true, true, {1, 1024})
		_ScanParam({"inline", inline}, {"boolean"}, false, true)

		if not (self.rawdata.fields) then
			self.rawdata.fields = {}
		end

		local field = {}

		field.name = name
		field.value = value
		field.inline = inline

		table.insert(self.rawdata.fields, field)

		return self
	end
	
	function self.addFields(...)
		local fields = {...}

		for _, field in next, fields do
			_ScanParam({"field", field}, {"table"}, false, true)
			self.addField(field.name, field.value, field.inline)
		end

		return self
	end
	
	return setmetatable(self, Embed)
end

return setmetatable(Embed, Embed)

with script

local new = mod.new()
new.addField("Name", "Value", true)
new.addFields(
	{name = "Name", value = "Value"},
	{name = "Name", value = "Value", inline = true}
);

it doesn’t give error, does anyone know why?

1 Like

Workspace.Script.ModuleScript:137: loop in gettable

I believe it means you have some kind of recursive calling going on with your metatables.

As a first attempt I would (still using your original script) remove this line:

And replace it with just

return Embed

I would also delete this line because I’m not sure why you’d want it, but you can keep it if you do:

Nevermind, I found a solution where inside Embed:addFields where there is

for _, field in next, fields do
	_ScanParam({"field", field}, {"table"}, false, true)
	Embed:addField(field.name, field.value, field.inline)
end

I change into

for _, field in next, fields do
	_ScanParam({"field", field}, {"table"}, false, true)
	self:addField(field.name, field.value, field.inline)
end

Before I return it with Embed.new(…) so in the main script you don’t need like Embed.new() but only Embed(), but yeah just for my preference.