I made a class module for roblox! :D

Do you hate metatables! Me too! I never want to touch it again, so I created a Class Module which significantly speeds up and simplifies making them.

local class = require(script.ClassConstructor)

local myClass = class()

function myClass:constructor()
	self.bool = true
end

local object = myClass:New()

print(object.bool)

It also fully supports metamethods! Just name the function the name of the method (Ex: __add)

local class = require(script.ClassConstructor) --First require the function (Yes this module only returns on function, not a table)

	local myClass = class() --You can also add a subclass, which will inherit methods and class variables. Ex: class(MyOtherClass)
	
	function myClass:constructor()         --Activates when the class is instanced! Useful for declaring variables.
		self.boolean = true
	end
    function myClass:__add(value: any)     --Make sure to add the ':' instead of just '.' as it will replace value with self.
		self.boolean = value
		return self                       --PLEASE PLEASE PLEASE Make sure to return self, this can cause lots of bugs if you forget!
	end

local object = myClass:New()
object += false --Output: object.boolean = false

Here is the package link if you’d like to try it: https://create.roblox.com/store/asset/85313922301150/ClassConstructor

Feedback is appreciated!

4 Likes

Did you make sure that the module is public?

4 Likes

Apparently you can’t publish packages to the creator marketplace, should be fixed now!