Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 7 years ago

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

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

1 answer

Log in to vote
1
Answered by 7 years ago

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 :)

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. TheeDeathCaster 2368 — 7y
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. TheeDeathCaster 2368 — 7y
Ad

Answer this question