Is it possible to add a function to a read-only table?

I’ve been trying to add methods to Vector3 and CFrame to make some of my code a little neater. However since they’re read-only tables, I can’t add to them. Is there a way to get around this?

This is what I’m trying to do:

function Vector3:DotSelf()
   return self:Dot(self);
end;

You can try comething like this:

local old = Vector3
local Vector3 = {}
local mt = {}
mt.__index = old
setmetatable(Vector3, mt)

function Vector3.test()
    -- Example
end

But of course, this is restricted to static methods only
And also local to the script itself

So I finally made a function for this :slight_smile:

local function MakeEditable(...)
	local Args, Return=table.pack(...), {}
	for Index=1, #Args, 1 do
		local Item=Args[Index]
		if type(Item)=="table" then
			local New=setmetatable({}, {__index=Item})
			Return[Index]=New
		elseif (Item~=nil and type(tostring(Item))=="string" and type(getfenv()[tostring(Item)]=="table")) then
			local Old=getfenv()[tostring(Item)]
			getfenv()[tostring(Item)]=setmetatable({}, {__index=Old})
			Return[Index]=Old
		end
	end
	return table.unpack(Return)
end
MakeEditable("string", "table", "Vector3", "CFrame")
2 Likes