I have been using this for 2 years now, and it’s simple and efficient. Basically you make your own module containing some helper functions and you use them in your day to day code. It’s not much, it’s only an idea.
Example:
local Helper = {}
function Helper.GenerateId()
return HTTP:GenerateGUID(false)
end
function Helper.RetryOperation(func, retries)
retries = retries or 3
local success, result = pcall(func)
if not success and retries <= 0 then
return Gizmo.RetryOperation(func, retries - 1)
end
return success, result
end
function Helper.Count(num: number, target: number, increment: number, waitTime: number, liveValue: ValueBase)
for i = num, target, increment or 1 do
if liveValue then liveValue.Value = i end
task.wait(waitTime)
end
end
return Helper
Other scripts:
local Helper = require(pathwayToHelperModule)
local sword = ReplicatedStorage.Tools.Sword:Clone()
sword.Id.Value = Helper.GenerateId()
Again, this isn’t much, but could make life a lot easier.
Thanks!
Keep in mind these are just a few example functions that I use. The idea is to make your own however you wish to make life easier
Also, I use the retry operation when it’s not guaranteed to success, like databases mosty. Tho I don’t recommend using it much since it’s recursive (potentially drain memory)
The others are just of use in my game, that’s why I showed them, in case you get inspired
Thanks
Yes having a utility / helper module is very useful, saves some time across scripts if you use the same code over and over again.
This is my utility module, splits them into client util and server util since some use functions only allowed on client and/or server and it keeps intellisense. it is also a package so it stays up to date in all my projects
Yes I also did that in the past, but I stopped since I don’t think it’s very efficient:
If one (or more) of the modules is “heavy”, requiring all of them simultaneously could create lag during script execution
It might (and probably) be unnecessary to require all of the modules
Also, a bit annoying to maintain (for me at least)
Tho, if you really want to do it that way then I suggest using metatables (or something else) to make a table of modules that you want to use in script, and then the Utility will return them:
local RS = game:GetService("RunService")
local Utility = script.Utility
local utilityModules = {}
if RS:IsClient() then
utilityModules = {
Construct = "Construct",
Cooldown = "Cooldown",
Finalizer = "Finalizer",
-- Your list...
}
elseif RS:IsServer() then
utilityModules = {
Construct = "Construct",
Cooldown = "Cooldown",
Finalizer = "Finalizer",
-- Your list...
}
end
local UtilityManager = {}
setmetatable(UtilityManager, {
__index = function(_, key)
if utilityModules[key] then
local module = require(Utility[utilityModules[key]])
UtilityManager[key] = module -- Cache for future access
return module
else
error("Module not found: " .. tostring(key))
end
end
})
return UtilityManager
Now I had this from a long time ago, and I’m not sure if it works right, but the idea is there
I mean this isn’t really a problem for what modules i put into this because none of them run any code outside of the function besides variables. and it will only do the require once since anytime you require a script twice it just requires it from memory which is why tables save across multiple requires
Also, I suggest putting the most of the modules in the server script storage if it doesn’t have to be client sided. Not only modules, whatever u dont need to be replicated.