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

I don't understand this part of module scripts. What does "return module" do? Help?

What does the "return module" do? I don't understand.

local module = {}
function module.bro()
	print("I need help!")
end
return module

1 answer

✓ Accepted answer

Let me explain this as best I can.

The first line of the script:

local module = {}

Creates a table called 'module'. Tables can hold anything. Integers, strings, functions, etc. When you do:

function module.bro()
    print("Help!")
end

You're adding a function into the table 'module'. That function is called 'bro'. So, now you have a table and in that table you have a function. What next? Well, you need this:

return module

When you're trying to access a module script you do:

local a = require(game.Workspace.Module)
a.bro()

That will print 'Help!'

When you require a module script it's actually running all the code inside of it. Here's the thing though, you're defining a table and putting a function inside and never calling it. So the only code that gets run is the return line. This just returns the table 'module' with any functions you may have defined. In this case the only function is 'bro'. That let's you access the table and everything in it Hope I helped :)

2 comments

0

Well, essentially (in a nutshell) the return operator returns the/ a value from the ModuleScript, like per say the table given; however, if you don't return the data via "return," it will not return anything.

0

So when a server-side or localscript calls upon, or "gets," the ModuleScript, it will return the data via b/c of the return operator in the ModuleScript; it "returns" the data.

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