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.
I require the module seprately.
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.
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.
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.
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.
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
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