ModuleScript - self.cameraobject returns nil

Here is a module I have been working on:

local PeripheralVision = {}
PeripheralVision.__index = PeripheralVision


function PeripheralVision.New()
	local self = setmetatable({}, PeripheralVision)
	self.CameraObject = Instance.new("Camera")	
	return self
end

function PeripheralVision:SetProperties(cframe, fov)
	local camera = self.CameraObject
	camera.CFrame = cframe
	camera.FieldOfView = fov
end

function PeripheralVision:GetProperties()
	local camera = self.CameraObject
	return camera.CFrame, camera.FieldOfView
end

return PeripheralVision

It’s not finished yet, because whenever I call .new() and then try to do :SetProperties() it returns a nil value.

The issue I’m running into is this: I create a new object called self.CameraObject in the first .new() function, and then when I try to reference self.CameraObject in another function it simply returns nil.

Does anyone know why this is? I’ve looked at other example moduleScripts like some of EgoMoose’s and I’ve done the same thing as them (at least i think I have) so I’m not sure why I can’t retrieve anything from self

The code is correct. I think the error is how you are accessing it in another function. Can you include the code you are using that returns nil.

Everything in the provided code is the module script.

these are the relevant functions:

local PeripheralVision = {}
PeripheralVision.__index = PeripheralVision


function PeripheralVision.new()
	local self = setmetatable({}, PeripheralVision)
	self.CameraObject = Instance.new("Camera")	
	self.CameraObject.Name = 'CameraObject'
	return self
end

function PeripheralVision:SetProperties(cframe, fov)
	local camera = self.CameraObject
	print(camera)
end

This is a server script that is doing the other stuff:

local pv = require(workspace.PeripheralVisionModule)

local peripheralVision = pv.new()

pv:SetProperties(script.Parent.POV.CFrame, 70)

The reason i’m just printing camera is so that I can see if it is nil or not. In this case it is nil

Edit: it errors on the line after SetProperties()

pv.new() is creating the new new object and returning it. You are accessing the modules table directly PeripheralVision (in the module script) which does not hold the index CameraObject only the returned table hodls this index.

-- just chaning the variable names around to be more logical 
local peripheralVision = require(workspace.PeripheralVisionModule)

local pv = peripheralVision .new()
-- using the new object returned pv call its function
pv:SetProperties(script.Parent.POV.CFrame, 70)

You can avoid this by changing the module to just return the creation function.

local PeripheralVision = {}
PeripheralVision.__index = PeripheralVision

function PeripheralVision:SetProperties(cframe, fov)
	local camera = self.CameraObject
	print(camera)
end

-- module now returns a function to which will create new objects
return function([any args here ect..])
	local self = setmetatable({}, PeripheralVision)
	self.CameraObject = Instance.new("Camera")	
	self.CameraObject.Name = 'CameraObject'
	return self
end

You can however still keep PeripheralVision.new if you wish though in my opinion its use case is very limited e.g a copy/clone function for duplicating the object.

1 Like

Thanks! I didn’t notice I was referring to the module and not the returned value. Works now.
I had some problems when trying your method of returning the function, it didn’t seem to return anything?
I tried printing from require and it just said function: 0x212353erga whatever and i tried calling some of the module’s functions and they didn’t work.

Using .new does work though, so thanks!

1 Like

with returning a function peripheralVision is now going the be the equivalent of using peripheralVision.new in fact you can also do the following which gets the index “new” that holds the creation function

-- module that returns a table we can index the key called "new"
-- new is the function and is assigned to the variable
local peripheralVision = require(workspace.PeripheralVisionModule).new

-- module that returns a function
-- the fucntion is assigned the the variable
local peripheralVision = require(workspace.PeripheralVisionModule)

-- both examples above can be equivalent equivalent

You can then use the above code to create new objects

local obj1 = peripheralVision() -- call the function which makes our new object
local obj2 = peripheralVision()
...
1 Like