How to add a .something() function to module script?

Im realy confused how I could add a function like this to a module:

require("Path to module script").something()

I tried doing it like this:

local module = {}
function module.something()
    print("Custom code here")
end
return module

But this will work only with the first function I make and wont let me delete it after my tests (It just keeps working even if I removed the function out of the script)

Can someone help me out?

Is it because im using the console to test it?

3 Likes

Have you tried doing this?

local module = require("Path to module script")

module.something()

If I understood you correctly

I need to know how i would add “module.something()”.
How would i add that to the module?

The same way you did it:

Chars

But that wont let me add more than one function.
As soon as I add another function I only get this error message:

require("Path").anotherthing():1: attempt to call a nil value

You could probably make a function that adds functions to the table.

function module.AddFunction(key, func)
	module[key] = func
end

It’s because I used the console to test it.
Looks like it wont update until you use it in a script or just
create the same script again.
Maybe require() does not work like i think it works :

Well lets say u are doing it wrong. I will show u how to do it.

Step 1:Get yourself a Script and a Module Script.

In the Module you should see something like this:

local module = {}

return module

Step 2: In the Script erase the print hello world and write this

local module = require(--where your module is stored
)

Now let me explain each word.

local module is defining a variable named module
(Guess u already know tht)
require is a function that allows you to sort of open a module in Luau (the coding language for roblox).now what you are doing is defining the variable to be a function. Now the another function is stored in tht module not the function you are writing. Instead just require the module and call any functions you like by doing

local module = require(--whereever ur module is
)

module.YourFunction(arguments_or_parameters_if_any)

module.AnotherFunction()

module:AnotherFunctionAgain()

I know how require() works.
But when I launch the same script in the console it will save it
once and NEVER update the module script it saved even if I rewrite the same cmd.
But why?

Oh…

Well thts just how it is actually :grimacing:.
You cant really do much about it. If you write anything in runtime and try to execute it wont work.try restarting the place as thts the only fix i know :sad:

Edit: Applies everywhere

Subsequent calls to require() on the same ModuleScript return a reference to the same table value.

local Script = script
local Module = require(Script.ModuleScript) --ModuleScript that returns an empty table constructor.
Module.Key = function() --Another script can 'require' this ModuleScript and call its 'Key' member.
	print("Hello world!")
end

I know about that. The reason I gave my solution is because OP said

so I thought they were doing something else.

Now that I think about it, my solution wouldn’t even work if the modules gave table copies when requiring. I should have thought about that before posting my solution.

You can avoid hacky stuff like recreating the script by hitting the “Reload Script” button in the “Script” tab.
It does not actually seem to reload, it rather seems to be applying the script to the .Source property of a script.

To save some memory, roblox doesn’t create a new table everytime a require is called. This means that the table saved to the given module won’t update until you leave the studio and reenter.

A simple fix for this is just to clone the module so it creates a new table for your use.

-- example
require(workspace.ModuleScript:Clone())

There’s a reload button. Just reload the script within the script menu.