2 Module Questions

Im requiring a module, but I need the module to do 2 things.

  1. How can I reference something from the script that’s requiring the module?

Like if there’s a string value in script 1 and script 1 requires the module, how can I find the string value in the script from the module?

  1. When the module is required, everything auto-happens, or do I have to return something?

You are going to have to return a table with string value, or a function that returns it, or the string value itself.

This question is unclear, can you elaborate.

Like if this is my modules script:

local table = {}
game.Workspace.Part.Anchored = false

Will game.Workspace.Part.Anchored = false auto happen as soon as the module is requirred?

Yes, but only once. Modules only run at most once in their lifetime. Their result is cached by require.

For instance if you had this:

local mod = { }
print("Hi")
return mod

If you were to require it twice, "Hi" would only print once, but the same table that mod points to is returned

1 Like

Yes, it will but the module will once run once. If you require it multiple times, it’ll just return the same result it did the first time it ran.

So modules are very interesting
I will give you a random module script example

local module = {
value1 = "Hello"
value2 = "World"
}

module:testFunction()
   print("Hello World Function!")
end

return module

In the server script, you could do something like this (assume the module script is a child of the script)

local moduleScript = require(script.ModuleScript)

print(moduleScript.value1) -- Prints "Hello"

for i,v in pairs(moduleScript) do
   print(v) -- Prints "Hello" then "World"
end

moduleScript:testFunction() -- Prints "Hello World Function!"