My Utility Module - do you do this? Pros and Cons? Feedback welcome

Hi all - Lately I’ve been using a utility module in my games - maybe you already do this. I have actions I take regularly in the module, and then I can reference them quickly in any script. Right now the utility module I’m building contains the following:

local utility = {}
--
--
function utility.c(table)--choose from table
		local randomNum = utility.r(#table)
		local choice =table[randomNum]
		return choice
end
--   u.d(character,"Jumping")
--
function utility.d(mainObject,target)--search for named item in table and destroy each one found
local searchTable = {}

	if mainObject:IsA("Table") then
		searchTable = mainObject
	else
		searchTable = mainObject:GetChildren()
	end

	for i,v in pairs(searchTable) do
		if v.Name == target then
			v:Destroy()

		end
	end

end
--
--
function utility.f(mainObject,target)--find tag within object or table   local isNPC = u.f(Damien,"NPC")
local searchTable = {}
local tagFound = false
	if mainObject:IsA("Table") then
		searchTable = mainObject
	else
		searchTable = mainObject:GetChildren()
	end

	for i,v in pairs(searchTable) do
		if v.Name == target then
			tagFound = true

		end
	end
	return tagFound

end
--
--
function utility.fhp(object)--find humanoid from part
local objectHumanoid = nil
local hasHumanoid = false

	if object and object:FindFirstChild("Humanoid") then
		objectHumanoid = object:FindFirstChild("Humanoid")
		hasHumanoid = true	
	elseif object and object.Parent and object.Parent:FindFirstChild("Humanoid") then
		objectHumanoid = object.Parent:FindFirstChild("Humanoid")
		hasHumanoid = true
	elseif object and object.Parent and object.Parent.Parent and object.Parent.Parent:FindFirstChild("Humanoid") then
		objectHumanoid = object.Parent.Parent:FindFirstChild("Humanoid")
		hasHumanoid = true
	end

return objectHumanoid, hasHumanoid

end
--
--
function utility.r(range)--random number
	local randomNum = math.random(1,range)
	return randomNum
end
--
--
function utility.v(typeOfValue,nameOfValue,valueOfValue,parentOfValue) --create a Value

	local newValue = Instance.new(typeOfValue)
	if nameOfValue then
		newValue.Name = nameOfValue
	end
	if valueOfValue then
		newValue.Value = valueOfValue
	end
	if parentOfValue then
		newValue.Parent = parentOfValue
	end

end
--
--
return utility

Then, in my scripts, I have the following:

local u = require(game.Workspace.UtilityModule)

So, then if I want a random number, I just enter this in the script:

local randNum = u.r(100)

that will immediately get me a number from 1-100.

But on a more time saving note, if I want to find out if the part that set off a Touched event has a humanoid, I just type:

local humanoid, hasHumanoid = u.fhp(touchingPart)

or, if I want to see if a character has an “NPC” tag, I can use:

local isNpc = u.f(someCharacterModel, "NPC"

but I can use that one to search for anything, in anything. So if I want to know if a Mesh part has a “cheese” tag set on it, I can enter:

local isCheese = u.f(theMesh, "cheese")

likewise, you can create any value on anything by typing:

local newTag = u.v("IntValue", "HitPoints", 20, character)

You can do this from any script at all, merely by typing the “require” statement at the top.

It’s pretty useful. What do you think?

1 Like

This… Is not ideal.

First of all, Your module functions have names like “fph” which will make it hard to remember what it does after you forget those functions.

Second of all, math.random(5) → A number from 1, 5

Third, Dont be THAT lazy. You can already use collectionservice for those.

9 Likes

That’s not how you check if an argument is a table, do:

if type(mainObject) == "table" then

to check if the argument is a table. :IsA() method is only for instances.

Why are you using single-lettered variable names? You have to read the module’s comments to know what they are every time you forget. It would be better if you were to put names for them which explains it.

For example, use Utility.FindTag().

You can do math.random(10) to get a random number from 1 to 10 so utility.r is unnecessary.

4 Likes

you’d be better off just using code snippets in vscode or something similar.

1 Like

You have the right idea with your module and definitely heading the correct way.

It can be very effective to create and maintain your very own Utility Module that can be used throughout your code.

1 Like

Single letter things really aren’t readable to us, others in the future who may find this post and even yourself if you’re not careful.

Big recommendation to use actual names instead if you are going to make another resource.

1 Like

I appreciate your response. I’ve been coding for a decade or so, and it’s pretty easy for me to remember “fph” is “find humanoid from part” because I do it SO much in games.

Likewise, I have no trouble remembering u.w is a wait that involves “tick” instead of “wait”. Nor do I have trouble remembering u.d is “search through table for an item, and destroy all instances of the item” - again because I do it so much that its pretty easy to remember.

Actually, I think I will be THAT guy - but I truly appreciate your input. Thanks for the response !

Wrote “actual names” for 5 years of coding roblox. I’m ready for shortcuts now. I appreciate your input. Hope your day is great !

Agreed - weird no one else here gets it but you! Cheers!

Thanks, I had actually just created this, and hadn’t tested everything yet. I caught that error, but really appreciate your help! Cheers !

I actually don’t have to read the modules contents everytime, it’s pretty easy to remember this. But for the longer functions, I think I’ll consider your advice. I like using

u.w(10) for a tick-based wait.

But I could use “u.FindHum” for find humanoid perhaps, It’s a much longer process and so I’d still be saving time to type it this way.

Thanks for the response, and advice about finding if a thing is a “table”… I actually hadn’t found this error yet, so that is a big help. I also had no ideas I could not use “IsA” on tables. Thanks for this tip - after 5 years of coding roblox, it’s pretty awesome to get a new tip ! Cheers !

You might remember those function names very well, but if you are intending this module to be a resource that could be used by others, then it is better to have self-explanatory names.

But if you are not uploading this for that reason then sure. “\_(:/)_/”

2 Likes

I agree. I was more curious how many use something like this, and what kind of feedback you folks would give. But I’m feeling I miscategorized this post now…shoulda maybe be moved to development discussion?

I use a my own module similar to this, but with different functions that have easier names to remember.

My module makes it so I don’t have to rewrite the same code over and over again.

Example:

local Utilities = {}

local MarketplaceService = game:GetService("MarketplaceService")

function Utilities:UserOwnsGamepass(player : Player, gamepassId : number)
    local ownsPass = false
   
    local success, result = pcall(function()
      ownsPass = MarketplaceService:UserOwnsGamepassAsync(player.UserId, gamepassId)
    end

    if success then
      return ownsPass
    else
      warn("Error when checking if the user owns the Gamepass:"..result)
    end
   return false
end

That’s why I love ModuleScripts so much. I can save myself so much time by not having to redefine everything.

1 Like

No, no it’s not. You don’t need a module for those methods, like why are you making a method just to use math.random?? And a method to make a BaseValue? Just use Instance.new for that.

1 Like

I also do this. Typically I only use it for super repetitive functions, like abbreviation of numbers, turning seconds into a timer such as 90 seconds into 1:30, and so forth.

I don’t see why anyone would disagree with a workflow this way. It helps keep your code clean and legible, and if you ever want to change the way a function works, you only have to change it in one script!

My only advice for you is to use more obvious names for your functions, it’s a good practice that you would already be used to if you ever begin working with other scripters.

2 Likes

To sum up what the others in this thread said, I recommend making the functions’ names more clear and make the module more efficient.

Some of these functions such as the utility.r can already do what math.random can do.

1 Like

I don’t agree with your statement on math.random
But I do agree theses names are very hard to remember.

1 Like

on vanilla lua,

1 Like

What’s the problem with math.random
?

1 Like

Oh gee I’ve wrote actual names for 7-8 years, you are never abd will never be “ready” for single letter variables, for they quite literally slow down your workflow if you take a moderately lengthy break.

2 Likes