Helper Module: Make coding easier

My fellow devs!

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! :fire:

3 Likes

The retry operation function does in fact look interesting, and could definitely be of use, other two is debatable, other than that, good work!

3 Likes

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 :muscle:

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 :sparkles:
Thanks :pray:

3 Likes

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

--> Services
local RunService = game:GetService('RunService')

--> Utility
local Utility = script.Utility

if RunService:IsClient() then
	return {
		['Construct'] 	   		= require(Utility.Construct);
		['Cooldown'] 	   		= require(Utility.Cooldown);
		['Finalizer'] 	   		= require(Utility.Finalizer);
		['Loader'] 		   		= require(Utility.Loader);
		['MathUtil'] 	   		= require(Utility.MathUtil);
		['NumFormat'] 	   		= require(Utility.NumFormat);
		['ProbabilityTbl'] 		= require(Utility.ProbabilityTbl);
		['Ragdoll'] 	   		= require(Utility.Ragdoll);
		['RayUtil'] 	   		= require(Utility.RayUtil);
		['Social'] 		   		= require(Utility.Social);
		['State'] 		   		= require(Utility.State);
		['TagHandler']	   		= require(Utility.TagHandler);
		['Spring'] 		   		= require(Utility.Spring);
		['MarketService']  		= require(Utility.MarketService);
		['StrokeAdjuster'] 		= require(Utility.StrokeAdjuster); 
		['Viewport'] 	   		= require(Utility.Viewport);
		['Shine'] 		   		= require(Utility.Shine);
		['Input']		   		= require(Utility.Input);
		['PlayerUtil']	   		= require(Utility.PlayerUtil);
		['AnimationController'] = require(Utility.AnimationController);
		['Runtimes'] 	  	    = require(Utility.Runtimes);
		['Bindables']	   	    = require(Utility.Bindables);
		['Destructor']			= require(Utility.Destructor);
		['toHMS']				= require(Utility.toHMS);
		['Signal']				= require(Utility.Signal);
	}
elseif RunService:IsServer() then
	return {
		['Construct'] 	   = require(Utility.Construct);
		['Cooldown'] 	   = require(Utility.Cooldown);
		['Finalizer'] 	   = require(Utility.Finalizer);
		['Loader'] 		   = require(Utility.Loader);
		['MathUtil'] 	   = require(Utility.MathUtil);
		['NumFormat'] 	   = require(Utility.NumFormat);
		['ProbabilityTbl'] = require(Utility.ProbabilityTbl);
		['Ragdoll'] 	   = require(Utility.Ragdoll);
		['RayUtil'] 	   = require(Utility.RayUtil);
		['Social'] 		   = require(Utility.Social);
		['State'] 		   = require(Utility.State);
		['TagHandler']	   = require(Utility.TagHandler);
		['Spring'] 		   = require(Utility.Spring);
		['MarketService']  = require(Utility.MarketService);
		['PlayerUtil']	   = require(Utility.PlayerUtil);
		['Runtimes'] 	   = require(Utility.Runtimes);
		['Bindables']	   = require(Utility.Bindables);
		['Destructor']			= require(Utility.Destructor);
		['toHMS']				= require(Utility.toHMS);
		['Signal']				= require(Utility.Signal);
	}
end

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 :muscle:

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

I mean yeah whatever you put in modules save across server-server and client-client, and if it works for you, then good job :+1:

For me… Well I can’t do that since most of my systems are made in modules :joy:

Yes, running any code could create lag. If your modules are inefficient, they may lag.

Mine are also made of modules and it works fine
image

1 Like

Well then nice :joy:

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.

Thats only the client stuff, the server stuff is in serverstorage
image