A question about ModuleScripts

Hello, I’m not sure if this is the right category to put this question in, but I have been analyzing a few Scripts I’ve seen developers using, and I keep seeing a pattern where people create ModuleScripts which are only used by one Script. I thought this was quite odd, so I tried researching about this, but everywhere I’ve searched has just said that ModuleScripts are only used when more than one Script requires them, meaning that the ModuleScripts they wrote wouldn’t make sense.

Normally, these ModuleScripts will contain things a Server or Client sided Script can do, and I’d just like to know if it is correct to implement them in a game, or if it is best to just use a normal Script for them (I’m mostly asking this because these ModuleScripts could perhaps be used to organize the code they are writing into more understandable and manageable pieces, but I feel as if that wouldn’t make that much sense since they would only be used for 1 Script).

Below you can find an example of one of these ModuleScripts I’ve seen (do note that this one is only used by one Script, which calls it to either detect if the Player left or entered a cave).

local module = {}

local lightning = game:GetService("Lighting")

function module.detectPlrAction(plrAction)
	if plrAction == "entering" then
		local colorCorrection = Instance.new("ColorCorrectionEffect")

		colorCorrection.Contrast = 0.75
		colorCorrection.TintColor = Color3.fromRGB(212, 212, 212)
		colorCorrection.Name = "CaveColorCorrection"

		colorCorrection.Parent = lightning
	elseif plrAction == "leaving" then
		if lightning:FindFirstChild("CaveColorCorrection") then
			game:GetService("Debris"):AddItem(lightning.CaveColorCorrection, 0)
		else
			warn("CaveColorCorrection not found")
		end
	end
end

return module
2 Likes

I feel like the whole point of module scripts is to organize your code, so even if one script is using the module and you know what youre doing, the code can become much more organized.

If your module only contains like one function, and only one script uses it, there isnt much reason to make a module script, but if you have entire systems in a module, or just use the module for storing information that all scripts can use, thats when its most effective to use them.

Along with that, if you just end up with a bunch of modules that have a single function or whatever, the workspace can get quite cluttered, so it may be best to just store the function in the main code.


In short, it doesnt really matter how many functions or whatever is in a module script, what ever works best for you to help your work flow.

Oh, that does make more sense now that you say it, to be honest using it in more complex systems probably is the best way to go (or in the most common way, when it is being called by other Scripts), well thanks for helping me out, I appreciate it!

1 Like