Potential Bug with Module Script?

From what I’ve learnt with Module Scripts, it should only return 1 value, most of the time a table instance. However, if you put a function in the module script, whether the module script is in or out the table, the function would run.

Furthermore, even if you delete the script and the module script, the module scripts would still run.

Video:

(Forgot to show you but, when trying to access the script via tapping the printed text, it says “Source not Found”)

Local Script

local moduleScript = require(script:WaitForChild("BugModuleTest"))

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("E was pressed using Script")
	end
end)

Module Script

local UserInputService = game:GetService("UserInputService")

local module = {}

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("E was pressed using ModuleScript")
	end
end)

module.pressQ = UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		print("Q was pressed using ModuleScript in table")
	end
end)

return module

When you require a module script, whatever is in the module script will execute. In this case, you’re attaching callbacks to the InputBegan event inside the module script so whenever you press those keys, those callbacks will run. It’s important to note that once a module script is required for the first time, the return value will be cached and used for future requires. This will prevent additional connections from being added to InputBegan.

As for you deleting the script and module script, the connections you added to InputBegan aren’t removed when the script is deleted (at least as far as I know). So even if you delete the scripts, InputBegan still sees those connections and fires the callbacks when you start an input.

Ah, this helps explain a lot, though, somebody has to go add that in the ModuleScript Documentation. Also, is there any way I can disconnect or “un-require” a module script? If not, then you don’t need to respond to this question.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.