Self is returning nil

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

2 Likes

Can you share the code where you call the MT:_EnablePayday function?

1 Like
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

1 Like

My module is in ReplicatedStorage, and i run this module in StarterPlayerScripts. Does have any problem with that?

put the print inside the loop and tell me what it prints
image

nil  -  Client - EasyEconomy:59

it printed this

1 Like

Self isint nil, Payday is. This can mean a few things, but the main one is that you most likely changed the Value in another script.

nope, i didn’t change value of Payday, please give me more reason

here is the run script(LocalScript)

local RS = game:GetService("ReplicatedStorage")
local EzEconomy = require(RS.EasyEconomy)

local Economy = EzEconomy._Start()

Economy:_InitRun()

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

you cant call a function with self like that. you are supposed to use the metatable

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:

MT._EnablePayday(self, true, Coin)

I need add this in function, or i need to add in callback of function?

Wdym?

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

image
image
image

see, this is what happens if you put it as self

image
image

OHHHHHHH, i see what you mean. your talking about calling the functions. hahaha, my bad bro. yea @platonarhipov listen to this guy

im sorry but…

ReplicatedStorage.EasyEconomy:59: attempt to index boolean with 'Payday'

im now understand that you can’t just add self in function, you can use self : typeof(yourfunction._Start()).