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

ModuleScript - How will it run?

Asked by 8 years ago

I haven't worked with ModuleScripts in a while, so I'm not sure exactly how they run. I know how to use them (Return something in the ModuleScript and use require() to access what has been returned) Yet I can't remember if the ModuleScript runs how I'm thinking it is.

Let me try to explain...

In this ModuleScript, I have this function:

return function()
    print(arg1)
end

In this LocalScript, I have this in it somewhere:

local arg1 = "This was an argument loaded from the LocalScript, or is the ModuleScript loading the code into me??"
local MS = require(game.ReplicatedStorage.ModuleScript)

MS()

So, does the ModuleScript just injects its code into the LocalScript? Or does it just run the function within the ModuleScript? I can't really do much of a test since my internet's down and I'm using my Phone.

1 answer

Log in to vote
0
Answered by 8 years ago

A ModuleScript can not get values from local values. A ModuleScript only returns values, which are created inside of the ModuleScript environment.

If you need to get values from the script that is require()-ing it in, consider putting them as function arguments or using getfenv to scrape the values from the environment. This assumes that the variables are not declared as local.

0
getfenv... Never heard of it! I'll check it out. Thanks! lightpower26 399 — 8y
0
Now, who in the name of fried Potatoes would downvote this? rexbit 707 — 8y
0
No idea, but somebody obviously didn't appreciate my answer despite not offering a better solution or anything constructive. User#6546 35 — 8y
Ad

Answer this question