How can I make a value in a module not replicate?

I am currently programming a system, the current problem I have is that values replicate between modules.

Except I want a single value to not replicate, as otherwise it effects the code.

Is there anyway to do this?

What do you mean by a value that replicates between modules? Do you have a value in a module script that should be different in each script that requires the module?

Yes that exactly!

Cause I think we all know this, but values stored inside of a module replicate between every module thats required it (at least on the server)

If you don’t want a single value to replicate, you can store that value inside a normal script instead of the module script

Yeah but considering what I am doing, making the user do it manually is the last thing I want to do (that and Im too lazy too do it manually)

The short answer is that you don’t. Modules are designed to run once, and only once, and will return the same value after the first run. You can’t change this behavior.

If you want different data per script it’s required in, I would use a faux OOP design where you can run Module.new() to create a newly initialized set of data you can modify without replication

1 Like

If you want to single out a specific value, then use the returned value of RunService:IsServer() in an if statement when the value you want to initialize is created. That lets you divide your initialized values into server/client. You can do the same thing for any function behavior.

local RunService = game:GetService("RunService")
local IsServer = RunService:IsServer()

local module = {}

if IsServer then
	--Value is initialized on server
	module.Value = 5
else
	--Value is not initialized on client
	
end

function module.Run()
	if IsServer then
		print(module.Value) -- 5
	else
		print(module.Value) -- nil
	end
end

return module

One of the main points of modules is to replicate values between all scripts.

I would recommend looking into OOP techniques to create a “self” object that can have different properties an example for a very basic zombie would look like this:

Zombie = {}
local ZombieModel = game:GetService("ReplicatedService"):WaitForChild("Models"):WaitForChild("ZombieModel")
Zombie.new()
    local self = {health = 100, model = ZombieModel:Clone()}

    setmetatable(self, Zombie)

	return self
end

Zombie:destroy()
     --garbage collecter here
    self.model:Destroy()
end

return Zombie

Then:

local ZombieMod = require(game:GetService("ServerScriptService").ZombieMod)

local zombie = ZombieMod.new()

-- it has property's and functions based of the individual object
print(zombie.health)

-- eg a garbage collector function. this would not replicate to other instances of the zombie
zombie:destroy()

Hopefully this is what you are looking for!

this doesnt work, as the value is different between scripts, setting module.Value overrides it in any other scripts that have required it.

…why would you do this? like there’s literally no point in doing this

I misunderstood the original question. Like junekept has already mentioned, there’s no way to have different instances of a module that multiple scripts are requiring. They only run one time on the first require. If your goal is to have different ‘packages’ of data from a single, that is the fundamental purpose of OOP and it is the only way you’ll achieve that.

yeah having different things isnt what I want, as its all one thing n stuff so yeah.

Ill leave this open just incase somebody knows a work around for a bit

There are many uses for this. For shared scripts in ReplicatedStorage, you can go down different routes depending on server or client initialization since it runs once on both. For example, having a Library module with access to all modules respective to server/client needs to have predetermined locations to search. If ServerScriptService needs to be checked for the server, and StarterPlayerScripts for the client, an if statement on RunService’s IsServer reduces the amount of components.

local RunService = game:GetService('RunService')
local IsServer = RunService:IsServer()

local Lib = {}

Lib.Data = {}
Lib.ServerScripts = {}
Lib.ClientScripts = {}


--|| SHARED ||--

--Require All Replicated Modules
for Index, Object in pairs(script.Modules:GetDescendants()) do
	if Object:IsA('ModuleScript') then
		Lib.Data[Object.Name] = require(Object)
	end
end

--Initialize Reference
for Name, Module in pairs(Lib.Data) do
	Module.Library = Lib.Data
end

if IsServer then 
	--|| SERVER ||--
	
	local S_Scripts = game:GetService('ServerScriptService'):WaitForChild('Modules', 5)

	--Find & Require
	for Index, Object in pairs(S_Scripts:GetDescendants()) do
		if Object:IsA('ModuleScript') then
			Lib.ServerScripts[Object.Name] = require(Object)
		end
	end

	--Add to main storage
	for Name, Module in pairs(Lib.ServerScripts) do
		Lib.Data[Name] = Module
	end

	--Initialize Reference
	for Name, Module in pairs(Lib.ServerScripts) do
		Module.Library = Lib.Data
	end

else
	--|| CLIENT ||--

	local SP_Scripts = game:GetService('StarterPlayer'):WaitForChild('StarterPlayerScripts'):WaitForChild('Modules', 5)
	
	--Find & Require
	for Index, Object in pairs(SP_Scripts:GetDescendants()) do
		if Object:IsA('ModuleScript') then
			Lib.ClientScripts[Object.Name] = require(Object)
		end
	end
	
	--Add to main storage
	for Name, Module in pairs(Lib.ClientScripts) do
		Lib.Data[Name] = Module
	end

	--Initialize Reference
	for Name, Module in pairs(Lib.ClientScripts) do
		Module.Library = Lib.Data
	end
end

return Lib



Oh, then would you be able to elaborate more on exactly what you want? I took from this that you wanted different values from your module.

Well basically every time a certain module I have is required, this number goes up by 1, and what I wanted to do is save this number in that module, but only that instance of it being required had access to it, so yeah

So if there were two different scripts that required the module that has the number, you want those two references to be linked to 2 separate values? Is this correct?

Yes thats it!

Try this as an experiment. It’s a basic constructor for OOP that does exactly that. Get 2 separate scripts and 1 module script. In the module have:

local Objects = {}

function Objects.new()
	
	local Info = setmetatable({}, {__index = Objects})
	Info.Stored_Number= math.random(1, 10)
	
	return Info
end

return Objects

In the other two scripts you can have:

Script 1

local ObjectModule = require(ModulePathHere)

local Object_1 = ObjectModule.new()
print(Object_1.Stored_Number) --Should be a random value between 1 and 1000

Script 2

local ObjectModule = require(ModulePathHere)

local Object_2 = ObjectModule.new()
print(Object_2.Stored_Number) --Should be a random value between 1 and 1000

This should give you two references to an instance created by the object module, which have different internal values.

If you’re looking for requiring itself to give you individual instances of a module, that isn’t possible. The behavior of modules does not allow that.