Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What are ModuleScripts?

1 answer

Let's look at a game.

Imagine that you have several scripts in the same game:

-- script 1
function doStuff()
	local p = Instance.new("Part", workspace)
	p.BrickColor = BrickColor.random()
	return p
end

doStuff()
-- script 2

function doStuff()
	local p = Instance.new("Part", workspace)
	p.BrickColor = BrickColor.random()
	return p
end

doStuff()
-- script 3

function doStuff()
	local p = Instance.new("Part", workspace)
	p.BrickColor = BrickColor.random()
	return p
end

doStuff()
-- script 4

function doStuff()
	local p = Instance.new("Part", workspace)
	p.BrickColor = BrickColor.random()
	return p
end

doStuff()

I am exaggerating, but isn't boring to copy and paste the same function to all scripts that need it?

The module script is used to reduce the lines you should write.

When you insert a module script, it looks like this:

local module = {}

return module

Now it's time to insert the code on the module script.

local module = {}
	function module.doStuff()
		local p = Instance.new("Part", workspace)
		p.BrickColor = BrickColor.random()
		return p
	end
return module

To make a script use the module content, you should use a require keyword:

local moduleStuff = require(--[[the location of the module script]])
moduleStuff.doStuff() --calls the function.

so, ModuleScripts are just made to help us be more comfortable at the scripting.

This discussion is preserved as it was. New answers, comments, and votes are closed.