Hey developers, recently I was lurking around in Roblox’s core PlayerModule
script that gets added to all Player’s PlayerScripts folders when running the game. I found out that when you require PlayerModule
it actually returns the initiated singleton instance instead of the PlayerModule
table.
See this code:
return PlayerModule.new()
It is present in the last line of that module.
As you know, Roblox makes a ModuleScript common by storing it in a common place in memory.
When this is first required by the require
method, It creates a new Singleton instance and returns it, but when trying to require it after first require, the already loaded module in the memory gets passed to the require
method so no new Instance is created.
So, after all that I saw some methods in that singleton class, The one in which I was mostly interested was GetCameras
method, As it returns the CameraModule singleton instance of the CameraModule
module-script present inside the PlayerModule
module script.
Now, When I called it, I expected it would return that singleton instead. But, it returned an empty table, Why was that, the code in PlayerModule
was relatively simple, there was no way that it could reset it back to nil or empty table. So I dug further, I manually required CameraModule
module script, and to my surprise, it returned, an, empty, TABLE!
Then I read the code inside that, and I found THIS AT THE END:
local cameraModuleObject = CameraModule.new()
return {}
Yes, AllYourBlox has made it so that when requiring this module script, no matter what, it’s gonna create a new Singleton instance and never return it, rendering it useless to other scripts, that GetCameras
method and even violating this statement in comments provided in PlayerModule
script.
Here’s what it says:
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.
I currently don’t know if it is intentional or not, but this violates what comment says, so, I think its a mistake and should be resolved. If you know anyone @ DevEngagementTeam or Dev Relations, You may wanna inform them about this. (If it isn’t intentional)