This is a perfect implementation for ModuleScripts! Essentially, a ModuleScript is like a script, but it doesn’t run when a server/client is started. Rather, it only starts when a ServerScript/LocalScript require s it, allowing it to run. This is perfect when you want more organization in your code and a more efficient programming structure.
ModuleScripts can also hold all different types of data, including functions, strings, numbers, etc. You can even place events in ModuleScripts!
Say I wanted to run some code after a certain event is fired, I can require a module and call a method/function that will help me achieve that task.
-- ModuleScript
local codeRunner ={}
function codeRunner.RunSomeCode(value)
-- run code here
if value == "hello" then
print("Good day")
end
return true -- I can even return values
end
return codeRunner
-- ServerScript
local codeRunner = require(my.codeRunner.Module)
value.Changed:Connect(function()
local success = codeRunner.RunSomeCode(value.Value)
if success then
print("It ran without errors!")
end
end)
TLDR; essentially a ModuleScript is a script that doesn’t do anything until a script require’s it, or “un-disables” it. It’s great if you want more organization in your code.
There shouldn’t really be a case where you have to Enable/Disable scripts for your game to work properly. That indicates that there is some underlying coding design problem.