Help accessing PlayerModule in a character script

I want to access the camera module object created by PlayerModule so i can use it. However when i try to use the modules GetCameras() method i get an empty table or sometimes an error saying that self is nil.
image

line 21 is return self.cameras
anyone got experience trying to mess with these?

here is the Module script that loads automatically on run time. ( for those who think that the issue is with the module script)

--!strict
--[[
	PlayerModule - This module requires and instantiates the camera and control modules,
	and provides getters for developers to access methods on these singletons without
	having to modify Roblox-supplied scripts.

	2018 PlayerScripts Update - AllYourBlox
--]] 

local PlayerModule = {}
PlayerModule.__index = PlayerModule

function PlayerModule.new()
	local self = setmetatable({},PlayerModule)
	self.cameras = require(script:WaitForChild("CameraModule"))
	self.controls = require(script:WaitForChild("ControlModule"))
	return self
end

function PlayerModule:GetCameras()
	return self.cameras
end

function PlayerModule:GetControls()
	return self.controls
end

function PlayerModule:GetClickToMoveController()
	return self.controls:GetClickToMoveController()
end

return PlayerModule.new()

…Self doesn’t exist I think.
Can you show entire script file that used “self”?

Did you use a “.” instead of a “:”? If self is what doesn’t exist then maybe you called the function with a “.”, which doesn’t pass self into the function.

its the default playerModule script that every player spawns with in their playerScripts folder. Load a game in studio and copy / paste it.

--!strict
--[[
	PlayerModule - This module requires and instantiates the camera and control modules,
	and provides getters for developers to access methods on these singletons without
	having to modify Roblox-supplied scripts.

	2018 PlayerScripts Update - AllYourBlox
--]] 

local PlayerModule = {}
PlayerModule.__index = PlayerModule

function PlayerModule.new()
	local self = setmetatable({},PlayerModule)
	self.cameras = require(script:WaitForChild("CameraModule"))
	self.controls = require(script:WaitForChild("ControlModule"))
	return self
end

function PlayerModule:GetCameras()
	return self.cameras
end

function PlayerModule:GetControls()
	return self.controls
end

function PlayerModule:GetClickToMoveController()
	return self.controls:GetClickToMoveController()
end

return PlayerModule.new()

I didn’t make the script. Its the default one roblox spawns you in with. They did use a period tho.

Note: Server can’t access to playerScripts

its from the client. Im able to get the module, its the methods that dont work. The error is with the Module script not with my script trying to access it.

Should say to Bug-Support group or wait for someone posts to bug category if it was came from Roblox’s script.

Or just copy and modify.

Yeah ive seen that post before i made this one. There wasn’t an solution to it though. I thought about bug reports but im not sure that anything wrong is happening. Im sure this is just me misunderstanding how that particular module works.

Or didn’t create instance before by using “.new”.

The module script returns a new instance at the last line of the script. when you require the module it will do that automatically.

You need to define self.cameras in your function first before returning it.

This is a module script. Self is defined in the constructor as a metatable. You dont need to define it in every function. the other modules parented this script do the same thing with no errors when required.

Create instance and call camera from that instance.

Or use [function](created instance)

Can you do that?

I meant did you use a period in your script that calls the function? The function :GetCameras() in the module script uses a colon, meaning it accepts “self” as its first parameter, so when you call it from wherever you call it, you need to either specify self by calling it with a colon or calling it with a period and just adding self as the first argument.

The script seems to be erroring because “self” doens’t exist, which means it’s not being passed into the function when you call it. Can you show your code that uses this module script and calls the function?

2 Likes

I didn’t know that, but i do remember trying it with the colon but the return value is an empty table.
in my script i require the PlayerModule and in a function i have PlayerModule:GetCameras() which prints out to an empty table.

Replace bolded string to created instance.

I dont get what you mean. What would i replace it with?

Okay, more laconic one.

--Create instance with
local pm = PlayerModule.new()
--or
local pm = require(PlayerModule)
--Then use
pm:GetCameras()
--and self will work

Note

function dsc:IsbookFound(pid,book)
--↓
ds.IsbookFound(dn,pid,dn)
function dsc.IsbookFound(pid,book)
--↓
ds.IsbookFound(pid,dn)

thats what i did. Just replace PlayerModule from my code with pm from urs. I only named it that way for convenience.