__index for functions

What do you want to achieve?

I’m currently working on a way to fix an error within my script, the end goal is to make a metatable where when certain functions are called it’d resort to the roblox method. (It’ll be easier to understand when you see my code)

What is the issue?

When calling the roblox function in my __index function it returns the error

Expected ':' not '.' calling member function GetChildren

What solutions have you tried so far?

I scrolled through the DevForums and found one post based on the same error I was having, however no solutions were provided, at least ones that would fit what I’m trying to accomplish.

Code

function Framework:SetupElement(element)
	return setmetatable({
		Element = element,
	}, {
		__index = function(self, index)
			if element[index] then
				return function(self, ...)
					element[index](...)
				end
			end
		end,
		__newindex = function(self, index, value)
			if element[index] then
				element[index] = value
			end
		end
	})
end

Now I understand why it’d be erroring, I simply don’t know how to go about fixing this.

Edit

I’ve also tried doing something along the lines of,

element[index](element, ...)

and received strange results

3 Likes

Solution

When using

element[index](element, ...)

I forgot to return.

Code

return element[index](element, ...)

1 Like