Module Doesn't Load After Dying

Hello. I’ve ran into a problem with my ModuleScript. When first spawning in it works all fine no problems. But when I reset or “Refresh” my character the Module doesn’t load again basically making the character useless. How could I fix this so it doesn’t do that anymore. Please help!

  • Code
-- Services {Game} --
local PlrService = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
-- Constants {Basics & Modules} --
local GameFronts = ReplicatedStorage:FindFirstChild('GameFronts')
local GameMods = GameFronts:WaitForChild('Mods')
local ModSufix = '_'
local MustLoadModules = {'Actions'} -- 'Note The Modules The Client Will Handle'
----

-- For Statement --
for _,Mod in GameMods:GetChildren() do
	if Mod.Name == ModSufix .. 'Actions' and Mod:IsA('ModuleScript') then
		-- Require Module --
		require(Mod)
	end
end
----

Put the Local Script that requires the module inside StarterCharacterScripts.

I thought the issue might have been that the module script was already required and it’s result was cached making it not run again (as in returning the old result).

1 Like

Correct. ModuleScripts will only run their top-level code the first time they are required in any particular execution context. An ugly workaround would be to change require(Mod) to require(Mod:Clone()), but the better solution is to require the module once, and have it return a table with a function that you can call repeatedly, rather than just running code and not using its return value at all.

1 Like

How would I achieve something like that?

Instead of your ModuleScript having something like this:

<code you want to run>
return <value you don't even use>

It can be as simple as this:

return function()
 <code you want to run>
end

Then you require this module once at the top of your script, like
local myFunc = require(<path to the ModuleScript>)
Then where you currently have require(Mod), you’d just have myFunc() instead.

Of course this can get a bit more involved if you were expected to save state between calls. Normally, people make a ModuleScript return a Lua table that has data members and/or one or more functions. But you can just have a ModuleScript like above that is just a function

1 Like

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