Trying to make custom methods for Instance

Why doesn’t it set the name?

trueinst = Instance

eh = {}

function eh.new(classname)
	local trueobj = trueinst.new(classname)
	
	local okay = {}
	function okay:Way()
		local realobj = getmetatable(okay).__index
		
		print(realobj.Name)
	end
	
	setmetatable(okay, {__index = trueobj})
	
	return okay
end

Instance = setmetatable(eh, {__index = trueinst})

local eh = Instance.new("Accessory")
eh.Name = "erer"
eh:Way()

Does it print anything? If it does, what does it print?

you define methods outside of the declaration of the object, not inside

It prints the original name of “Accessory”.

If truobject would be a new instance, and okay would just be the index of that. Then realobj would also inherit the name of trueobj, which is just the ClassName of the object you tried to index. So index is returning the new instance’s name not the actual objects.

ᶜᵃⁿ ʸᵒᵘ ᵉˣᵖˡᵃᶦⁿ ᶦᵗ ᵐᵒʳᵉ ᶜˡᵉᵃʳˡʸ

Since okay is the index metamethod of trueobject which is a new instance of the ClassName you used in eh.new it would have the same name as the ClassName of whatever instance type you referenced eh.new with.

1 Like