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)
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?
Well thts just how it is actually .
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
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())