Extremely confused on modulescripts

I don’t understand how module scripts work.
So every time you create a new module script it has

module = {}

return module

I am very confused upon this. Is module suppose to be a writable table?
This is my module. It is suppose to be a global table.

local module = {}

local consonants = {

"b",

"c",

"d",

"f",

"g",

"h",

"j",

"k",

"l",

"m",

"n",

"p",

"q",

"r",

"s",

"t",

"u",

"v",

"w",

"x",

"z"

}

function random()

table.insert(module,consonants[math.random(1,#consonants)])

end

return module

UGHH I JUST DONT KNOW HOW TO USE IT IM SO FRUSTRATED ITS SO HARD TO CONCENTRATE I DONT EVEN WANT TO WRITE NOW

2 Likes

Modules don’t have to return a table but that is most popular since after all, modules are meant to be reusable libraries of code.

random is essentially a “private” function in the module, so it isn’t exposed to requiring scripts if that was your question.

But yes the table can be written to.

1 Like

ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require .

Due to this functionality of ModuleScripts by the time you require this ModuleScript it’s contents can not be altered during the runtime. So having a function that tries to randomize the return value won’t work. You would need to instead intergrate this table into the function and have that function return a value, not the module.

local module = {}

local function module.Function()
   --Code
   return Value
end

return module

So whenever you need this value you would just call the function.

1 Like

First thing is you can’t ask someone to fix scripts for you, if you want you can hire someone. Second thing is keep your language professional, only related stuff is needed.

And about your problem, you need to look at the above answers properly.

1 Like

ModuleScripts can be used for various things. I mainly use them for global functions and tables/arrays.

Getting a ModuleScript’s contents.

require(id or moduleObj)

Global function.

module.Test = function() -- replace module with base table that it is returning.

end

Fire a function in a script.

Module:
```
module.testFunc = {}

if typeof(testFunc) == 'function' then
testFunc()
end

Script:

module.testFunc = function()

end
```

modules are hard to understand.
Most of people just use them as a table, like in a script.
You can run normal code in a Module too.

I am literally about to burst right nowimage
I get this error

imageimage

that’s going to cause a big stack overflow but ok haha

This won’t do anything as you did not call the function. You would need to do

function module.random() --you can also use module:random()

table.insert(module,consonants[math.random(1,#consonants)])

end

image


Consonants is a table in the consonants modulescript.

Freaking out on a public forum is very embarrassing. Don’t do that.

When you require() a ModuleScript, it runs whatever code is contained, and then returns whatever you return at the end of the ModuleScript. The default ModuleScript contains an empty table, and then returns that empty table. When you call require(), the return value of require() is whatever your module returns (e.g. a table containing a bunch of functions declared within the ModuleScript).

You should declare the random() function in the ModuleScript as part of this table so you can access it when you require the ModuleScript. The same goes for the consonants table if you want it to be accessible when you require the module.
function module.random() ... end
module.consonants = { ... }

2 Likes

The consonants table needs to be a part of the module table. You are returning the module table, not the consonants table.

But I thought that you could get the variables in a module script by requiring it, I think I saw it done before

When you require a module, you only get what the module returns. If you want to access the variables in a module, you must declare those variables as members of a table that the module returns.

1 Like

But it doesn’t work in practise

Get rid of the local in it then.

You should read more carefully. Your syntax is not correct as Studio is pointing out; you do not need the local on this line because module is already declared as a table, and you don’t need to declare a table member as local.

image

Just do module.consonants = {}

All further accesses to consonants must be done by accessing module.consonants, not consonants.

3 Likes

Now you need to replace consonants with module.consonants. See where I am getting at?

local je = module.consonants[math.random(1,#module.consonants)]

Like this.

If you need anymore information, visit the developer hub: Scripts | Documentation - Roblox Creator Hub

1 Like

Oh. My. God.
Thank you so much, it finally worked. Now I am laughing hysterically in real life.

1 Like