Hello everybody, im making a module for economy system. And i use OOP to make clear code of modulescript. And i use self keyword to access a specific value(like for example get payday value). And i testing my game, every value from self get nil. And i tried all methods to get value from self keyword, it’s doesn’t work. But i will say that OOP is hard to learn for me. Because from other functions i don’t get a specific value from keyword ‘self’. Please help me to fix this bug, i need learn how use self.
ReplicatedStorage.EasyEconomy:60: attempt to perform arithmetic (add) on number and nil - Client - EasyEconomy:60
local EE = {}
local MT = {}
MT.__index = MT
function EE._Start()
local self = setmetatable({},MT)
self.Payday = 250 :: number
self.CooldownPayday = 15 :: number
return self
end
function MT._InitRun()
MT._CreateLeaderstats(game.Players.LocalPlayer)
end
function MT._CreateLeaderstats(player : Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
MT._CreateCoin("Money",player,Leaderstats)
end
function MT._CreateCoin(NameOfCoin : string, player : Player, leaderstats : Folder)
local Coin = Instance.new("IntValue")
Coin.Name = NameOfCoin
Coin.Value = 0
Coin.Parent = leaderstats
MT:_EnablePayday(true, Coin)
end
function MT:_EnablePayday(Enable : boolean, Coin : IntValue)
if Enable then
print(self.Payday)
while true do
Coin.Value += self.Payday
task.wait(self.CooldownPayday)
end
else
warn("Payday is disabled")
end
end
return EE
function MT:_EnablePayday(Enable : boolean, Coin : IntValue)
if Enable then
print(self.Payday)
while true do
Coin.Value += self.Payday
task.wait(self.CooldownPayday)
end
else
warn("Payday is disabled")
end
end
return EE
I believe what he was asking was where in the script you use the module; you call :_EnablePayday()
there is nothing necessarily wrong with the module itself so it has to be somewhere from where you call the function
Oh. I wasn’t aware you were calling the function from the metatable itself
Here’s your problem, you’re doing MT:_EnablePayday()
so the self value would be the Metatable instead of the actual table with the value
you can fix this by just adding self args to _initrun, _createleaderstats, and _createcoin
or you could call the method from the local script instead of nesting all these
i was changed all of this real quick, before you reply this. Guess what? it doesn’t fix it.
function MT:_CreateCoin(NameOfCoin : string, player : Player, leaderstats : Folder)
local Coin = Instance.new("IntValue")
Coin.Name = NameOfCoin
Coin.Value = 0
Coin.Parent = leaderstats
MT:_EnablePayday(true, Coin)
end
also i changed a _initstart, _CreateCoin, and _Createleaderstats, and function _EnablePayday run in createcoin function that run in createleaderstats that run’s in initrun
Because you’re still calling it from the metatable…
you need to call it as self:_enablepayday()
which wouldn’t work in that function because you don’t have self defined
This isn’t true, if self is an instance with it’s metatable assigned to MT, it will automatically call that method while also passing self. Alternatively, you could do this:
Anytime you are calling a function which requires the object, like the enable payday function, you can do this: self:DoStuff(arg1) or MT.DoStuff(self, arg1). Both are equivalent expressions, the : is just a syntactical sugar so you don’t to specify self in the parameters of a function.
ive been using metatables for a while and you never use self to make a function. it doesnt even make sense because self wont have existed and itll error out