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

Csn constructors exist in a module object?

Is this code legit in Lua?

local obj = {}
obj.new = function()

end
return obj

2 answers

✓ Accepted answer

Yes, it is completely legit. However, you may find it productive to simply return the constructor function unless you have a good reason for needing a table where the only member is a 'new' function.

1 comment

0

Thanks a lit! Now I can make new APIs for my models of the game

You can do so.

local obj = {}
obj.__index = obj
obj.new = function(val)
	local newobj = {}
	setmetatable(newobj,obj)
	newobj.val = val
	return newobj
end

Now, you can create an object of that class.

b = obj.new("string")

Finally, print out the key called val.

print(b.val)

string

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