Module script Issue (Need Help)

I seem like haven’t understanded how the module script work.

I just want to ask you guys for help about the solution, I should use to require the script from module.

This is what I thought and used.

  1. I require the module seprately.

  2. I require every other module script into one module script, and I only require from the main one module.

3.(I’m using) I just require the module script every time I need in new script.

  1. IDK if this will work or not , I want to use one main script and require some of module that mostly used and put it in to Gobal Table (_G)

If you guy can help , Please help me and explain me about it or give me some sourse that i can find more explanation about it and the best method to require module script.

All i worry about is will it cause the lag to the game if i require the same module script again and again multiple time.

Thank For Help.

Requiring the module will give you what the modulescript returns.

-- module:
local data = {}

data.Ammo = 10
data.MaxAmmo = 10
data.Bloom = 0.5

return data
-- localscript or script:
local Pistol = require(path.to.module)
print(Pistol.Ammo) --> 10

Keep in mind, that this module only runs one time on the server, and one time on the client. That means, the same exact data is given to any script that requires it.

So if I require it multiple time it won’t cause the lag?

Yes. Only the first time you require it will the script run. That means, if the script uses yielding functions like task.wait(), it will also yield any script requiring it until it has finished.

I have one more question, Should I put multi-Function in one module or Should I Separate it into many module?

You should put relevant functions in the same module.

local Register = {}

Register.Cash = 0

function Register.add(money)
    Register.Cash += money
end

function Register.clear()
    local c = Register.Cash
    Register.Cash = 0
    return c
end

return Register
1 Like

I have one module script that help me for shortcut some code. it like Extrascript Module. But it have over 500 lines+. Should i Seperate it or keep it

if you are getting errors, make sure the modules don’t require modules

For instance, if module1 requires module2, and in order for module2 to load it has to require module1, that will cause an error

1 Like

Once modules are required to save cyclic lagging and even memory, roblox saves the data that was returned from the module alongside the module so if you call require again it will return the same table (if you are using tables), of course this doesn’t work across the client-server boundary.

-- vanilla lua 
local module = require('/modules/module.lua') 
local module2 = require('/modules/module.lua') -- both require the same file

print(module == module2) -- false
-- this is because the module data is ran again instead of being cached
-- roblox lua
local module = require(workspace.ModuleScript) 
local module2 = require(workspace.ModuleScript) -- both require the same ModuleScript

print(module == module2) -- true
-- this is because the original data is saved for when its required again
1 Like

Thank for the help, Now I understand abot it better. :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.