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?