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

What Strange Behaviors Occur with Module Scripts?

Asked by 4 years ago
Edited 4 years ago

I am about to use module scripts with roblox for scripting since it seems to be much organized here are a few questions.

****What happens when two scripts require at the same time?

I have read that it causes weird behaviors but I couldn't replicate it. Maybe just maybe it might replicate in game so just double checking.

****Variables are global

Is there a possible way that I can give each script its own local variable?

****Can the function run at the same time?

Wondering if I call it at the same time, would it stop one or can both of them run at the same time?

This would help a lot thanks!

2 answers

Log in to vote
1
Answered by
compUcomp 417 Moderation Voter
4 years ago

1) This is an extreme case, and I wouldn't rely on its behavior. I doubt that this could happen if you aren't purposefully trying to recreate this.

2) I'm not sure what you mean by that. The point of a ModuleScript is that identical code can be used in more than one place. If you want variables specific to a script, define the variable inside the script, not the ModuleScript.

3) Please clarify. Do you mean different scripts requiring the same module, then calling a method of that module at the same time? In that case, they would run simultaneously.

If I am grievously wrong, please correct me. I wouldn't describe myself as a beginning scripter but my knowledge of ModuleScripts could use some dusting off.

0
yea seems good. You answered all of them. Thanks! superbolt999 36 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

lets say you have 3 module scripts module A,module B, and module C.

if module A requires module C, and Module B also requires module C and A. The code wouldn't run at all.. There won't be errors or anything. When I first encountered this situation, I literally debugged the code for 6 whole hours. reading the code over and over and over again, and adding a bunch of print statements everywhere.. basically if a module requires another module which requires another module that the previous module requires, then it causes weird behavior

No variables are not global if declared in module scripts.a module script can return a value which can be accessed by whatever script that required.. if the returned value is function, then that function can access all the variables in the module. doesn't matter where its called from.

Yes, a function can run multiple times. the following is valid;

In a module script:

local module = {};
local count = 0;
function module.countdown()
    for i = 10, 0, -1 do
        count = count + 1
        print("call #"..count, i)
    end
end

in a script:

local module = require("the module above")
while wait() do
    coroutine.wrap(module.countdown)();
end

Answer this question