Object Orientated Programming Problem

I’m currently writing a gun system, and I’ve decided it would be a lot easier to convert it to oop, as such I’ve run into a issue (as per usual) I’ve never really done this before.

Code:

local framework = {}
framework.Arms = {}
framework.Camera = {}
framework.Gun = {}
framework.Animate = {}
framework.PlayerExtras = {}

framework.__index = framework

function framework.New(gunName)
	local new = {}
	setmetatable(new,framework)
	
	new.Rig = gunModules:FindFirstChild(gunName):WaitForChild("WeaponRig")
	new.WeaponFolder = gunModules:FindFirstChild(gunName)
	new.LastPlayedAnimation = "nil"
	new.ADS = false
	
	return new
end

(All my functions use “:” and when i try call self from them self just returns the functions name)

(If needed I’ll send how I invoke the module)

Any help would be apreciated.

1 Like

I don’t see the problem here. It just seems like you’re posting your code.

Can be more specific about the issue you are having?

Show me your framework functions and describe the functionality you wanted versus what is actually happening.

“(All my functions use “:” and when i try call self from them self just returns the functions name)”

function framework.Animate:PlayAnimation(AnimationName)
	if self.LastPlayedAnimation ~= AnimationName then
		self.LastPlayedAnimation = AnimationName
		
		local animationData = require(self.WeaponModule.Modules.AnimationData)
		animationController:Play(AnimationName)
		
		local player = game.Players.LocalPlayer
		local char = player.Character or player.CharacterAdded:wait()
		
		char.Humanoid.WalkSpeed = animationData[AnimationName].WalkSpeed
	end
end

and when print self, in return i get the functions name.
image

Yep, that’s essentially what self does.
Here is a link to the method

I swear i read somewhere that using self in oop it returned the table that called it.

As far as I can see, there is no error with your code, just a misunderstanding of the way self is interpreted by Lua.

In Object-Oriented contexts, self refers to the instantiation of the object class, used in class function definitions. In the normal Lua context it refers to the parent table of the method.

It is working exactly the way it should, from the looks of what you posted.

1 Like

Alright, I’ll try something else, thanks for clarifying. :slight_smile:

I made a video on classes if you want to learn what self means.

So self is a reference to the object. So if you are in a table and inside of a function in that table call self it will reference that table.

Code example:
Table = {

A = 1;

B = 5;

C = 5}

function Table:Test()

print(self.A) ---> 1

end

Table:Test() --- 1

Now refereeing to proxys and metatables, they all work the same. They are really just big dictionary’s with OOP functionality.

Here is an example of a proxy

image

image

Here is an example of a metatatable
image

They are very similar.

To wrap up self references the object or table that it is being called in.

Just figured out my problem, my functions were incased in a table inside the actual framework table, therefore it was just looking at the table it was in instead of the framework