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

Does requiring a module script more than once effect the events in it?

Let say I have a module script, with an event in it.

local Event = Part.Touched:connect(function(Hit)
	print("A")
end)

And now let say I need to require that module in more than one script.

--Script1
local A = require(Module)

--Script2
local B = require(Module)

Will the event get connected two times? Firing both times when a player touch the brick or only once?

1 comment

0

Exactly what I was looking for, thanks.

2 answers

When you require a modulescript for the first time, it will run the contents of the script.

It will then save the thing that gets returned by it, and attaches it to the module.

Any later require will just return that thing -- it will not re run code. This is normally what you want.


If you want separate requires to be re-doing any work, you should return a function (or an object with methods/functions) that the client will call:

-- MODULE
return function()
	print("Do thing!")
end

-- SCRIPT
local a = require(mymodule)

a() -- Do thing!
a() -- Do thing!

It can;t essentially, because what you have set up is a connecting function. Just calling the script wont set off that function, since its waiting to activate once 'Part' is touched.

Hopefully this is explanation makes sense, if not let me know and I can expand more. :)

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