So, I’ve read this post: Do modules keep running after death? - #4 by reteach
And I am working with a modular structure, here is how it’s setup currently:
I will be focusing on the client side instead of the server side because I feel like that’s where I should be focusing most in this post. Inside of the ClientMain Script, I require the Boilerplate Module in the “Shared” folder like so:
--ClientMain Script
local Boilerplate = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Shared.Boilerplate)
Boilerplate.Main()
local Modules = Boilerplate.Modules
local SharedModules = Boilerplate.SharedModules
local Test = Modules.Test
Test.Main()
The Boilerplate module handles the variables the server and client will access, it also handles the Client variables, like Player, UserInputService, Character, and etc. It also has a table called “Modules”, the current module I’m working with is called “Test”, as you can see in the Client folder I have a ModuleScript called “Test”. Inside of the Test Modulescript, there is a main function like so:
--Test ModuleScript
local Test = {}
local Boilerplate = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules").Shared.Boilerplate)
local Modules = Boilerplate.Modules
function Test.Main()
Boilerplate.UserInputService.InputBegan:Connect(function(Input, gameProcessed)
if not gameProcessed then
if Input.KeyCode == Enum.KeyCode.F then
print("F Key!")
end
end
end)
end
return Test
Inside of this Main function I make a new event, a InputBegan event and I listen for the “F” Key to be pressed. To get to the main point, whenever I Destroy the ClientMain localscript inside of the Character, the InputBegan event doesn’t fire anymore. Whenever I press “F”, it doesn’t print “F Key!” anymore. (Which works entirely in my favor, I’m just wondering why this actually happens, hence why I’m making this post) This makes me wonder, if ModuleScripts actually don’t keep running after death, or is the Event being garbage collected when the Script that required it is nil or destroyed? (If I didn’t explain things well or you need me to elaborate on a few things, please let me know).
Edit: Also, here is my Boilerplate Module Code:
local Boilerplate = {}
function Boilerplate.Main()
--[[Setup Boilerplate Variables]]--
Boilerplate.Players = game:GetService("Players")
Boilerplate.RunService = game:GetService("RunService")
Boilerplate.RepStorage = game:GetService("ReplicatedStorage")
Boilerplate.RepModules = Boilerplate.RepStorage:WaitForChild("Modules")
--[[Setup Module Tables]]--
Boilerplate.Modules = {}
Boilerplate.SharedModules = {}
Boilerplate.ModuleDirectory = {}
--[[Setup Client/Server Module Directory]]--
if Boilerplate.RunService:IsServer() then
Boilerplate.ServerStorage = game:GetService("ServerStorage")
Boilerplate.ModuleDirectory = Boilerplate.ServerStorage:WaitForChild("Modules")
elseif Boilerplate.RunService:IsClient() then
Boilerplate.ModuleDirectory = Boilerplate.RepModules.Client
--[[Setup the Client's Variables]]--
Boilerplate.Player = Boilerplate.Players.LocalPlayer
Boilerplate.Character = Boilerplate.Player.Character or Boilerplate.Player.CharacterAdded:Wait()
Boilerplate.UserInputService = game:GetService("UserInputService")
end
--[[Insert Respective Modules of either Client/Server & Shared into Modules Table]]--
--Client/Server Insertion
for i,v in pairs(Boilerplate.ModuleDirectory:GetChildren()) do
if v:IsA("ModuleScript") then
local State, Error = pcall(function()
Boilerplate.Modules[v.Name] = require(v)
end)
if not State then
warn("[!] "..Error)
end
end
end
--Shared Insertion
for i,v in pairs(Boilerplate.RepModules.Shared:GetChildren()) do
if v:IsA("ModuleScript") then
local State, Error = pcall(function()
Boilerplate.SharedModules[v.Name] = require(v)
end)
if not State then
warn("[!] "..Error)
end
end
end
end
return Boilerplate
(This is the same Setup as this post’s solution, however with some little changes)