Module don't want to "replicate" it's contents to other required scripts

When you require module script, it becomes activated, and runs it’s code once.

But somewhy, I did a module which not follows that rule and instead reruns each time I require it. (Assume that I call if from server only, ik that it won’t replicate across client-server boundary)

local RS = game:GetService("ReplicatedStorage")
local Remotes = RS:WaitForChild("Remotes")
local RunService = game:GetService("RunService")
local IsServer = RunService:IsServer()

local CurrentId = 0

local Data = {}
local Module = {}
local Initialized = false

function Module.AddItem(Key, Value)
	local KeyValue
	if IsServer then
		local DataId = Key:GetAttribute("DataId")
		if not DataId then
			KeyValue = CurrentId
			Key:SetAttribute("DataId", CurrentId)
			CurrentId += 1
		else
			KeyValue = DataId
		end
	else
		KeyValue = Key
	end
	print("Adding key:", Key, "; " .. tostring(KeyValue) .. ";", Value)
	Data[KeyValue] = Value
	if IsServer then
		Remotes.ItemDataChangeEvent:FireAllClients("AddItem", KeyValue, Value)
	end
	return KeyValue
end

function Module.GetItem(Key)
	return Data[Key]
end

function Module.RemoveItem(Key)
	print("Removing key: " .. tostring(Key))
	Data[Key] = nil
	if IsServer then
		Remotes.ItemDataChangeEvent:FireAllClients("RemoveItem", Key)
	end
end

function Module.GetAllData()
	print(Data)
	warn("Items's table name is: " .. tostring(Data))
	return Data
end

if not Initialized then
	Initialized = true
	print((IsServer and "Server" or "Client") .. " has begun initialization of data table")
	if not IsServer then
		Remotes.ItemDataChangeEvent.OnClientEvent:Connect(function(Action, ...)
			Module[Action](...)
		end)
		local LastData = Remotes.ItemDataTransmitFunction:InvokeServer("GetAllData")
		for k, v in pairs(LastData) do
			Module.Data[k] = v
		end
	else
		Remotes.ItemDataTransmitFunction.OnServerInvoke = function(Player, Action, ...)
			return Module[Action](...)
		end
	end
end

print("Module activated")

return Module

In this case, “Module activated” gets printed as many times, as I require module.

BUT: in the same time, when I require the following module:

local CurTime = os.clock()
print("Module activated")
return CurTime

It decides to print that only once.

WHY IN THE WORLD IT BEHAVES LIKE THAT???

If you require a module, it gives you new information, if you require it again somewhere else, it will give you new information too, the information doesnt replicate.

1 Like

Wrong statement, try to utilize my second module from diffirent places. Try even print what that module returns after couple of waited requires. They will give same result:
image
image
image
image

It prints once because the print is not inside any function, put it in where you install the module for example: module.Install()

1 Like

Huh. I must have been modules wrong this whole time :sob:
Well good to know Im wrong

1 Like

If module returns function, then function will be returned. When you will call it - ofc it will print more times - but even if that function will refer to that CurTime variable, result won’t change:
image
image
In my case, I return table of functions, which should work with “pre-defined” data, as I assume, but it decided to use diffirent laws of luau

I had that misunderstand of modules for a whole 2.5 years too.

1 Like

Okay so just realized that You can’t detect the require multiple times since the module is only running once. Once the module is required the same information to the same value is returned.

1 Like

That’s what I was talking about. I used this approach for dozens of my scripts, and they worked without flaws.
But this specific script has began to glitch out with being re-required every time I require it, instead of returning what it has done once.

Also, Idk by which laws in the world, it works later. After all requires. So each require resets it, and it works with last one.

The only way to detect the continues printing, is by putting it inside the main function, otherwise I’m not sure if they are any another methods.

1 Like

Where is the ModuleScript stored?

This module is stored in ReplicatedStorage

1 Like

ModuleScript | Documentation - Roblox Creator Hub

ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require.

1 Like

Then why in the hell it reruns every time I require it?

Exact same system were working for around 20 my modules, but this specific module has decided to not work as other ones!

1 Like

This will yield the requiring of the script allowing other requires to think they were the first to call it. Perhaps use task.delay to allow the require to complete, or instead of running the code inline, use a separate method which can be called immediately after.

1 Like

My script is being used by both client and server. In the case of server, this method never will be used, but script is still getting problem. Even if I completely remove script content, problem persists.

Hm, I guess I found possible reason why this thing occurs…
Some of my script use Actors (which idk how to force working anyway), and they are requiring that module too. When I removed actor, it stopped glitching away.
Now only one glitching module which requires problem left - pickup.

So, yes, problem of that code was Actors.
But what if I’ll need to use actors to speed up code?
Idk…

Scripts that run under Actors will be executed in their own separate VM (you can think of it as its own environment) for the sake of thread safety when running code in a parallel state. Requiring a ModuleScript and having it cache a result from within an Actor will actually just cache that result in the VM the actor is executing under, and vice-versa from the global one - you unfortunately cannot use them interchangeably as you would in normal scripts.

You should look into cross-thread communication or research other methods of cross-actor communication (alternatively, if you aren’t running computationally intensive tasks that would otherwise benefit from multi-threading, you can ditch using actors all-together).

1 Like

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