Self returning nil from module?

Hello,sorry I am very bad at explaining but I’ll try to explain the best I can!

I am trying to practice and use metatables for the first time. I am using this for help and reference. Everything seemed alright when I doing this, but when I tried to call self.exposure, it returned nil unless I re-value it like this

self.exposure = 2

But if I try keeping the value I give it from the module, it just returns nil.


--**MODULE**--
local LightingEffects = {}
LightingEffects.__index = LightingEffects

function LightingEffects.Activate(ExposureBrightness, Saturation, Contrast, Blur, showNightVisionLight)
	print("Active")
	local newEffects = {
		
		exposure = ExposureBrightness,
		saturation = Saturation,
		contrast = Contrast,
		blurness = Blur,
		
		showNightLight = showNightVisionLight,
	}
	return setmetatable(newEffects, LightingEffects)
end

function LightingEffects:AddEffects()
	print("adding")
	local exposure = self.exposure 
	print(exposure) --> The value should be at the value I set it at, but instead, it's nil.
end

return LightingEffects

--**SCRIPT**--

local module = require(path)

module.Activate(1, 2, 3, 4, false) --exposure, saturation, contrast, blur, bool
module:AddEffect() --> returns nil ?

When I do it like this, it just returns nil, but when I, again, forcefully change it, it will change. For example;

function LightingEffects:AddEffects()
	print("adding")
	local exposure = self.exposure 
    exposure = 2
	print(exposure) --> returns 2
end

I hoped this made sense, any help would be appreciated! (Don’t be scared to ask for more clarification :sweat_smile:)

You need to run the functions from a variable. Let me show you.

local foo = module.Activate(...)
foo:AddEffects()
1 Like

That was fast, can you explain what this means though?

This won’t change anything, the issue is how he’s calling the function.

1 Like

What do you mean by this exactly?

Oh wait nvm I read that wrong. Let me try that

I’m sorry I’m replying a lot, but why does this work?

OH yeah my bad, I didn’t read the message clearly.
I thought his self doesnt exists.

1 Like

Well, before you were just running the function without passing the object, aka self, which is what the first function returned.

1 Like

Oh I get it now, thank you so much for the help!

1 Like

If you want to learn more about self then you can read more about it here:

1 Like