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

ModulScript only runs once per call?

Hi ScriptingHelpers Community,

I'm attempting to create a hotel room door which can be purchased by other users, but won't give them the source code.

I've successfully created a module which runs all the functions needed by the model, but for some reason there's an unusual error which I can't get my head around.

If I try and run the script twice in one game, it will only run once. Please see the example below for some context.

Example:

**}**Player 1 swipes their room key on Door 1.

**}**Door 1 opens.

**}**Player 2 swipes their room key on Door 2 a short time after Player 1.

**}**Door 2 opens.

**}**Player 1 and Player 2 swipe their keys at the same time at both Door 1 and Door 2.

**}**The door which was swiped first opens, the other does nothing.

**}**Once the door has completed the opening function, the other door functions again.

I've tried looking at global functions, but I can't see any relevant materials which I can see that relate to my issue, either that or I'm just being blind.

Thank you in advance.

1 answer

✓ Accepted answer

This is simply how ModuleScripts work.
From the wiki: *"Its code is executed at the point it is required, and will only be executed once per peer (server/client)."*

To avoid this, you can wrap the ModuleScript's code in a function which you can call directly after requiring. This function would contain the module's original body, and return whatever you initially had it return. Example:

(Module)

return function()
    --// The module code
end

(Usage)

require(location)() --// Note the parentheses; this requires the module then calls the function it returned.

Hope this helped.

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