Question regarding Modules

I am not really experienced with developing in Roblox as I have started just a few months ago and recently got into learning about Module scripts.
So I know some basics as I have read the API reference. When I got into youtube to learn some more I came in front of things like Mouse Modules and FastCast and there might be several so I may need to learn a lot more.
I started using the mouse module by BRicey instead of
player: GetMouse() {I think this function is unsafe as I have seen on the API reference page though it works}
Do these modules need really advanced scripting knowledge to create and how can we approach making one?

I don’t really understand which category{scripting support and development discussion} this should fall into but because it is related to scripts I am putting this on scripting support.
Thanks

A module itself is easy. You just make a table or function in it, and return it.

local Settings = {
FoV = 70,
WalkSpeed = 16,
Ammo = 160
}
return Settings
local Settings = require(SettingsModule)
print(Settings.Ammo)

A module is just a table with functions:

local MouseModule = {}

function MouseModule:GetMouse()
	-- Do stuff
end

return MouseModule

Now you can use the module:

local MouseModule = require(script.Parent.MouseModule)

local mouse = MouseModule:GetMouse()
1 Like

A module is quite easy. Basically, a module is a table getting returned. You can insert something into the table.

local module = {
game.ReplicatedStorage.Ball
game.ReplicatedStorage.WedgePart
}
function module.Instance()
module[1]:Clone().Parent = workspace
module[2]:Clone().Parent = workspace
end

return module

Then you have a script that calls it and the functions.

local module = require(game.ReplicatedStorage.InstanceSript) -- you've called the module
module.Instance() -- calling the function

I’m not sure whether or not this needs to be added, but I felt like I should. You can attach functions to tables anyways without a ModuleScript. The definition of a ModuleScript is not “a table with functions attached”. It is “a Lua source container that runs once and must return exactly one value.”

local foo = {
	bar = function()
		print('baz')
	end,
}

foo.bar2 = function()
	print('baz2')
end

foo.bar()
foo.bar2()

The reason, however, that ModuleScripts are so commonplace is because they make organization & not repeating yourself incredibly simple. A thread that I recommend you learn with alongside teaching ModuleScripts to yourself is this one:

This thread will do a good job walking you through attaching functions to tables & then why integration with ModuleScripts is so important.

3 Likes