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

How make a metatable variable return an Instance?

How would I make the Metatable variable return something other than the table?

Example = setmetatable({}, {
	__index = function(self, i)
		return workspace
	end
})

print(Example) 

Like to make Example return a instance

2 answers

In your instance, You would do

print(Example.someObscurlyNilIndexThatWillMakeTgisReturnWorkspace)

10 comments

0

I need the variable of Example return something other than a table...

0

Can you explain further what you are trying to do?

0

Because, http://puu.sh/8aG3t.txt I cannot use just the variable of a created object to parent a object to it....

0

So I'm not entirely understanding so bare with me. When you call an index from ObjectController... It returns something like a property, correct? So you want to just return the object instead...?

0

In this case Instance.new("Part").Parent = Instance.new("Part") errors due to the metatable variable returning a table value. I was wondering if it was possible to change it so the variable doesn't return a table but a Instance/Userdata instead.

0

Heh... I'm sorry to say this but ROBLOX limitations with what they lock does not allow this to happen. You have to probably add another index to the object that is equal to the Instance.

0

Meh, I guess I will have to custom code my own fenv... Wish me luck I'm going in deep i~i

0

Are you asking for a way to create a userdata? Use newproxy.

0

Can I point said userdata to an Instance :D?

0

Well, what he is doing is creating a kind of replacement for the Instance table with metatable extensions. If anyone knows a solution, give a call.

You cannot do this. However, if you supply a __call metamethod that returns your instance, you can use Example() to get the instance.

Example = setmetatable({}, {
    __index = workspace,
    __call = function() return workspace end
})

print(Example())
--> Workspace 

We used to be able to edit the metatables of in-game objects, but they have been locked for a very long time.

Edit: Alternatively, you can check out my small project and see if it suits your needs.

3 comments

0

Not practical for my purpose of needing it.

0

You can take my Custom Hierarchy model and see if it suits your needs.

0

I just need it for the variable. :/

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