Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

ModuleScript - How will it run?

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

✓ Accepted answer

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.

3 comments

0

getfenv... Never heard of it! I'll check it out. Thanks!

0

Now, who in the name of fried Potatoes would downvote this?

0

No idea, but somebody obviously didn't appreciate my answer despite not offering a better solution or anything constructive.

This discussion is preserved as it was. New answers, comments, and votes are closed.