I want to set a variable in my object-oriented framework
When I print (self.Animator), it prints “nil” and every other variable I set.
--// Framework Stuff
local Framework = {}
local FrameworkMT = {__index = Framework}
--// Constructors
function Framework.new(Player)
local self = {}
self.Character = Player.Character
self.Animator = self.Character:WaitForChild("Humanoid").Animator
return setmetatable(self, FrameworkMT)
end
function Framework:Equip(WeaponName:string)
if Disabled then return end
--//
print(self.Animator)
end
Either that object doesn’t exist in the humanoid, or your calling the function without taking self. So basically, when you call a function without it taking self, it won’t recognize it exists until you declare the argument first.
Make sure that when you call :Equip() you’re calling it from the object returned from Framework.new() and not calling it from the required Framework ModuleScript itself.
He did create the animator. If you look at the way he calls Equip you’ll notice that the wrong object is passed as self. He needs to call the function from the object he created.
Can you simply do print(self) and see what it prints?
There should be an option to display the entire table in output, if you don’t have that enabled already.
I’m fairly certain I know what’s going wrong, but I need a bit more information.
I mean this is a weird class setup anyways, try this:
--// Framework Stuff
local Framework = {}
Framework.__index = Framework -- This is fine, and rather common OOP paradigm.
--// Constructors
function Framework.new(Player)
local self = setmetatable({
_child = true, -- // This is a child (instanced) object.
}, Framework)
self.Character = Player.Character
self.Animator = self.Character:WaitForChild("Humanoid").Animator
return self
end
function Framework:Equip(WeaponName:string)
-- // And just in case...
if self and not self._child then error("Cannot call :Equip(WeaponName: string) on the Base Framework object! Use Framework.new(Player) to get a new Object!", 2) end
if Disabled then return end
--//
print(self.Animator)
end
return Framework
--// Framework Stuff
local Framework = {}
local FrameworkMT = {__index = Framework}
--// Constructors
function Framework.new(Player)
local newFrameworkMT = {}
newFrameworkMT.Character = Player.Character
newFrameworkMT.Animator = newFrameworkMT.Character:WaitForChild("Humanoid").Animator
return setmetatable(newFrameworkMT, FrameworkMT)
end
function Framework:Equip(WeaponName:string)
if Disabled then return end
--//
print(self.Animator)
end
return Framework
local module = {}
module.__index = module
module.New = function(player)
local self = setmetatable({}, module)
self.disabled = false
self.player = player
return self
end
module.Equip = function(self, weaponName)
if self.disabled == true then return end
print(self.player.Character.Humanoid.Animator)
end
return module