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)
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.
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.
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