I need help with OOP

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Using self so I don’t have to pass through the object again and again

  2. What is the issue? I’m not sure how to reference the module script to actually run the function.

  3. What solutions have you tried so far? I’ve looked on here couldn’t find anything that would help me

EDIT: First time I’ve used OOP so sorry if this is a bit confusing/simple answer.

--Example:
object:Serialize()
--But how would i reference the module script to actually run that?
--module script
function module:Serialize(des)
	local itemID = self.ItemId.Value
	local pos = {["X"]=self.Position.X,["Y"]=self.Position.Y,["Z"]=self.Position.Z}
	local ori = {["X"]=self.Orientation.X,["Y"]=self.Orientation.Y,["Z"]=self.Orientation.Z}
	local name = self.Name
	local parent = self.Parent
	local anchored = self.Anchored
	if des == true then
		self:Destroy()
	end
	return {itemID,pos,ori,name,parent,anchored}
end

You have to require the module:

local module = require() --Module path here
module:Serialize() -- Arguments are here

I don’t recommend OOP for this because there isn’t a point and it will do more harm than good. OOP should be used when it’s supposed to, like custom properties and objects.

1 Like

Yeah like what TOP said I was attracted to OOP because of how cool it looked but like procedural generated code is just as good and efficient and effective as OOP Code.

By the way, if you want a dirty way of serializing and deserializing CFrames (youre using position and orientation, CFrame should achieve the same for you I suppose?), you can do this:

local function serializeType(o)
    return typeof(o) .. '.new(' .. tostring(o) .. ')' -- Vector3.new() --> "Vector3.new(0, 0, 0)"
end

local function deserializeType(o)
	local startIndex, endIndex = o:find'.-%.new%(';
	if startIndex and startIndex == 1 then
		local classType = o:sub(1, endIndex - 5);
        local classCreator = getfenv()[classType];
		if type(classCreator) == 'table' and classCreator.new then
			return classCreator.new(unpack(o:match'%b()':sub(2, -2):split', '));
		else
			error('Unsupported type for decodeType: ' .. tostring(classType));
		end;
	end;    
end

This also supports values other than Vector3s and CFrames (UDim2s etc).

Anyway; when calling a function through a table with : rather than . (t:foo() and t.foo()), the only difference is that t is automatically assigned as the first argument in function foo when called with :.
For example:

local myTable = {
    myFunction = print
}

myTable:myFunction() --> prints myTable (well, its memory address), because myTable is automatically assigned as the first argument.
myTable.myFunction() --> prints nothing

Another example would be like so:
table:foreach(print)
This automatically assigns the global table to table.foreach, and is the same as table.foreach(table,print).