Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to get __metatable function to be called?

My goal

Get getmetatable to return nil by a function I've assigned a __metatable field to.

Code:

local tbl, mt = {}, {
    __metatable = function()
        return nil;
    end
};

setmetatable(tbl, mt);
print(getmetatable(tbl))

Problem

The call getmetatable(tbl) is literally evaluating to the function itself and not calling it,

Is there a way to get this to happen, or would I just have to use some other value? I'd really prefer nil being the output, but if not then oh well.


Please answer only if you know what you are talking about. Or else ur answer's gonna get bombarded with downvotes LOL

8 comments

0

no

0

you can run the function inside the table by saying print(getmetatable(tbl)())

0

It does print the function memory address, but I want getmetatable to return the result of the function which is nil

0

so you don't want to do getmetatable(tbl)(), you want to call the function when you get the metatable, not afterwards?

0

No i want it to return nil, i want getmetatable to return nil

0

You could do the following, not sure if theres a way to have the function inside the table since Lua will error because it's expecting a metatable https://pastebin.com/ZqCj1ejL I'll look into it some more if that isn't okay

0

just set __metatable to nil

0

Not gonna work since it won't even add the field

1 answer

The __metatable field takes any value, not just a function. Furthermore it will NOT cause getmetatable() to call the function you assigned to it. It will simply return the function instead of the metatable, but not call it.

2 comments

0

I know it can be anything, but i was looking for a way to get the function to be called. Is my only option to rewrite set/get metatable to do this?

0

does the pastebin option work? if not, why?

This discussion is preserved as it was. New answers, comments, and votes are closed.