Accessing a module script from multiple scripts at the same time?

I was working on a system to store player values such as ‘damage’, ‘defence’, ‘cooldowns’ etc inside a module script to make them changeable and accessible but I encountered an issue. I can’t access multiple module scripts at the same time? When I read browsing the wiki on them I came across this:

Note that the first call to require on a ModuleScript will not yield (halt) unless the ModuleScript yields (e.g. calls wait ). The current thread that called require will yield until a ModuleScript returns a value.

I tested this by creating two instances of the following script:

– Local Script (Creates two instances of):

local RemoteEvent = game:GetService("ReplicatedStorage").TestEvent

local function OnInputBegan(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F then 
		print("Text says", script.Parent.Text)
		if game.Players:FindFirstChild(script.Parent.Text) then 
			RemoteEvent:FireServer(script.Parent.Text)
		end
	end
end


UserInputService.InputBegan:connect(function(input, gameProcessedEvent)
	OnInputBegan(input, gameProcessedEvent)
end)

– Server Script (Did not create another instance of):

local RemoteEvent = game:GetService("ReplicatedStorage").TestEvent
local req = nil


local function ChangeValue(player, Text)
	print(player.Name)
	req = require(game.ServerScriptService.AllStats:FindFirstChild(Text))
	req.Defence += 10
	print(req.Defence)
	print("Printed Defence")
	req.Damage(Text, 10, 0, nil)
end


RemoteEvent.OnServerEvent:Connect(ChangeValue)

– This is what req.Damage located inside my server script does:

local PlayerValue = {}

PlayerValue.Debuffs = {}
PlayerValue.Buffs = {}
PlayerValue.Damage = 1
PlayerValue.Defence = 10

PlayerValue.Debuffs = {"Test"}  

function PlayerValue.Damage(Target, Damage, Modifier, ReceivedFrom)
	local ReceivedFrom
	local Target = game.Workspace:FindFirstChild(Target)
	local Humanoid = Target:FindFirstChild("Humanoid")
	if PlayerValue.Defence > 0 then 
		print("Player defence not depleted")
	else 
		print("Player defence depleted")
	end
	
	print("Damage ordered", tostring(Damage))
print()
end



return PlayerValue

And it only ran it once when I pressed F, whereas it should duplicate it and run it twice as I had two versions of the local script. Did I make an error when trying to test to see if I interpreted the developer wiki right or is this how module scripts function and is not circumventible? That’s annoying if so…

Not really sure what is going on.

This is bad practise. You should call require as less as possible and therefore put it at the top of your script.

1 Like

This is for a different use case.

I explained it in How would I handle dynamic modifiers such as cooldowns, damage, defence etc? this link

I’ll describe what I’m doing in simple terms here:
A module script that stores defence, cooldowns, damage, etc so I can dynamically change the players abilities cooldowns, their defence, how much damage they deal allowing me to add modifiers which buff/debuff all of those different variables. Upon player join a template module script is cloned and stored inside ServerStorage where its values can be accesses. The template looks like this:

local PlayerValue = {}

PlayerValue.Debuffs = {}
PlayerValue.Buffs = {}
PlayerValue.Damage = 1
PlayerValue.Defence = 10

PlayerValue.Debuffs = {"Test"}  

function PlayerValue.Damage(Target, Damage, Modifier, ReceivedFrom)
	local ReceivedFrom
	local Target = game.Workspace:FindFirstChild(Target)
	local Humanoid = Target:FindFirstChild("Humanoid")
	if PlayerValue.Defence > 0 then 
		print("Player defence not depleted")
	else 
		print("Player defence depleted")
	end
	
	print("Damage ordered", tostring(Damage))
print()
end



return PlayerValue

I forgot to include what my actual require did, sorry for that. Original post should be updated by the time you read this.

Not sure what’s going on. You should totally be able to this. I whipped up a quick test:

script1 and script2 in a button in startergui:

local button = script.Parent
button.MouseButton1Click:Connect(function()
	print(script:GetFullName(),"Firing Event")
	game.ReplicatedStorage.RemoteEvent:FireServer(script.Name)
end)

recieve in ServerScriptService:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,name)
	print(script:GetFullName(),"Recived event from",name)
	local mod = require(game.ReplicatedStorage.ModuleScript)
	mod.test(name)
end)

ModuleScript in ReplicatedStorage:

local module = {}

function module.test(...)
	print(script:GetFullName(),"running stuff for:",...)
end

return module

output on clicking the button: