2 scripts requiring same module, function in that module should be called only once. What's best way to do this?

Hello guys. Let’s say I have 2 regular scripts and 1 module script:

--Script 1
local Module = (ReplicatedStorage.Module)
Module.Initialize()
--Script 2
local Module = (ReplicatedStorage.Module)
Module.Initialize()
--Module
local Module = {}
function Module.Initialize()
    Module.CreateProblem()
end
function Module.CreateProblem()
    RunService.RenderStepped:Connect(function(Delta) ... end)
end

In the simplified example above, you can see that 2 scripts require same module script, and it will create 2 RunService connections. But what if I need only 1 RunService connection? What’s best way to do this? Make some attribute variable and set it to true after first connection? Restructure code (how?)?

Extra notes:

  1. That’s simplified example. There’s possiblity of 10s of scripts requiring such module.
  2. Problematic function should be called only once.
4 Likes

Call initialize with some parameter set to true in one script

Then the intialize function only calls the create problem function if that parameter is true

3 Likes

You could check if the RenderStepped Connection has already been made.

--Module
local Module = {}
local RsConnection

function Module.Initialize()
    Module.CreateProblem()
end
function Module.CreateProblem()
    
if not RsConnection then RsConnection=RunService.RenderStepped:Connect(function(Delta) ... end) end
end

If you only want an entire function to run once then

--Module
local Module = {}
local FunctionActive=false

function Module.Initialize()
	
	Module.CreateProblem()
	
end

function Module.CreateProblem()
	
	if FunctionActive then return end
	FunctionActive=true
	RunService.RenderStepped:Connect(function(Delta) ... end) 
	
end
3 Likes

When module required, it’s code not shared. Due to this, if single module was called twice, it will be executed from beginning twice, resulting in doubled connected functions. That’s why I did this topic. Everything isn’t that easy.

2 Likes
local module = {}

local Module = {}
local isProblemCreated = false

function Module.Initialize()
	if not isProblemCreated then
		Module.CreateProblem()
		isProblemCreated = true		
	else
		warn("Module.CreateProblem() has already been called.")
	end
end

function Module.CreateProblem()
	-- Your existing code inside CreateProblem function
	print('Problem')
end

return Module
3 Likes

Will be recreated each time when module was required. Won’t work.

3 Likes

I am testing it right now and only print problem once.

2 Likes

Hm, seems strange to me, because when I tested in game, module have decided to run code twice. Simpler test-case like your really gives valid result. I’ll try to look why :thinking:

1 Like

You’re wrong. ModuleScripts are only loaded into memory once.

3 Likes

Maybe you want to write a ModuleLoader that way you will have greater control. As the ModuleLoader can keep tabs on require() calls.

2 Likes

Are you requiring from the client and server and only trying to have it run once?

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.