Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you pass a value to a Module Script?

Asked by 5 years ago

I am currently trying to learn modules so I can use them correctly and effectively, but there is a single line in both the developer and the wiki that reads:

Developer: "If a ModuleScript is required multiple times in the same environment the return value from the first execution will be used for every require call. "

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

  1. This means Module Scripts are purely static and will never change? (like a read-only table)

  2. If that's true, then you can never have code that uses the result of a ModuleScript to perform a comparison operator since the result will always be the same?

  3. math.random cannot be used inside them?

  4. If all of that is true, then I have to ask: why use them? Regular functions and Remote Functions can/will do more. The only things ModuleScripts really seem useful for are storing large amounts of declarations, various constant data, and storing static designs (such as using a script to build something).

  5. Or do I have this all wrong because you can send values to a ModuleScript? I looked everywhere but couldn't find a template on this.

  6. To get around this couldn't you clone the Module Script before you require it, change its name, then destroy the one you required? (Granted this may only work if you only require the Module Script from a single point, and should probably just use a normal function) 6a. If you can, couldn't you use this to effectively hide ALL of your code by uploading it to Roblox and using the assetID in the require? Or will that cause massive lag since you'll have to receive data outside of the Server every time you require?

Sorry for so many questions, I'd rather just lump all of them into one.

1 answer

Log in to vote
1
Answered by
seith14 206 Moderation Voter
5 years ago
Edited 5 years ago

Using a ModuleScript Can be very useful for a whole lot of things like Organization,Storing Data,returning data. I mainly use it to Organize but I'm sure there are tons of more uses.

You can store data inside a module script, You can also send things to a function for example: Server Script

local plrs = require(script.ModuleScript)

game.Players.PlayerAdded:Connect(function(plr)
    plrs.newp(plr)--Sending to Module which I'm going to set a indexed plr data table
    print(plrs.checkdata(plr))--Prints the table's Cash that I'm returning
    print(plrs.randomnum())
end)

Inside Module Script

local module = {}
local plrs = {} -- The plrs data indexed table

function module.newp(plr)
    plrs[plr] = {['Cash']=400} -- Setting The table with Cash inside
end

function module.checkdata(plr)
    return plrs[plr].Cash -- Returning
end

function module.randomnum()
    return math.random(0,4)--returns a number
end

return module

So yes you can use math.random() and store data

Also if you put it inside ReplicatedStorage it could be useful for both Client/Server.

0
Thank you! I ran a while loop with a counter on print random number and it worked exactly the way I wanted to use it. I understand my erorr now, the module will only run once, but you can use any function inside of it multiple times if you point directly to it. This answered all of my questions. SteelMettle1 394 — 5y
0
Glad I can help seith14 206 — 5y
Ad

Answer this question