Which functions should I add to my ModuleScript?

Hello people of the dev forums!

I am working on a ModuleScript to contain nearly every function a scripter will need in their code.

Sounds like a tedious job I know, but I’m up for it

So far I’ve added string functions (string.format() etc), services, math (math.random() etc) and ray functions.
I also added a GetWorkspaceObject and GetObjectInService function which searches descendants for the object which is acquired from the argument.

I’m asking what functions would you like to see in it & which do you use the most?

15 Likes

I would create a function called :MakeRay(start,direction)
It would create a ray, with the following parameters. If direction is a Vector3 value, then it calculates the direction by doing (start - direction).Unit * 300, (Like how you would script a gun)

Alright I’m on it! Thanks for the quick reply!

1 Like

Welding between two parts
ex:

function module:WeldBetween(part0, part1)
    local w = Instance.new("Weld")
    w.P0 = part0
    w.P1 = part1
    w.C0 = part0.CFrame * part1.CFrame:inverse()
    w.Parent = part0
    return w
end

Another useful function would be a method to get a list of characters given a collection of parts (such as when you use workspace:FindPartsInRegion3()
ex:

function module:FindCharactersInList(list)
    local chars = {}
    local foundChars = {}
    for _, part in next, list do
        if part.Parent:FindFirstChild"Humanoid" and not foundChars[part.Parent.Humanoid] then
             foundChars[part.Parent.Humanoid] = true
             chars[#chars+1] = part.Parent.Humanoid
        end
    end
    return chars
end

(Returns an array of characters, no duplcates)

I’m so glad someone is doing this! I though about doing something like this but never got around to doing it. I don’t know if this fits into the idea you had for the module but, you could add a leaderstat maker. Or maybe a easy way to create a new instance. Something module:Make(type, parent). I know that this is kind of pointless as it would be easier to use Intance.new. But, with Instance.new you have to put the parent after the obj is instantiated. But with this function you could shorten it to make it a little bit faster. I know that I would use it but I don’t know if it would help anyone or if it would be even effective. It’s just an idea. :slight_smile:

Great ideas! Thanks, I’ll be sure to them @GamingBroCoolkid328 @RogueMage

2 Likes

Instance.new(instance, parent) is valid.
https://developer.roblox.com/en-us/articles/Create-Parts-via-Code

@GamingBroCoolkid328
Instance.new(instance, parent, CFrame) might be nice

:Clone() however does not have the option
so a
:Clone(parent) and
:Clone(parent, CFrame) are options

It isn’t efficient to do this. It is much more efficient to create the Instance, set all of its values, then set the Parent.

This article explains it in detail: Article

3 Likes

Yes, this is why I said the function would set the parent to make it easier than instantiating the obj then later setting the parent.

1 Like

Not sure if it would fit every scripter but I’d definitely find a utility which hooks into ChatService and allows you to send messages to players without having to deal with channels and speakers useful :slight_smile:

Finished on the leaderstat creator, you can now make leaderstats with ease!
image

2 Likes
function module:GetModelMass(model)
   local Mass = 0
   
   for _,descendant in pairs(model:GetDescendants()) do
      if (descendant:IsA("BasePart")) then
         Mass = Mass + descendant:GetMass()
      end
   end

   return Mass
end
1 Like

Interesting, good to know.
Thank you.

Not a function, but separate your utilities by type/task/so on so developers can salvage and use parts independent of the grand ModuleScript. Cluttering all kinds of functions into a single ModuleScript can and will get annoying not only for you as a maintainer but a developer as a user of this as well.

2 Likes

Cool! Hopefully it will prove useful! Also, do you have a estimated time for when the module will be finished?

Something I have in my module that I’m sure will be useful:

A function that returns the server type; normal, vip or reserved. If it’s a vip server potentially also the server owner. If I had access to my laptop right now I’d provide my code but it’s a bit late.

1 Like

Extra table functions. Such as contains and count (for tables that contain strings for keys)

It may be a bit better because the second argument of Instance.new is not recommended to use (according to a DevForum article). Upon reading this article, I completely ended my habit of using the second argument. It would be a very simple function to run. (The article is from around two and a half years old, so I may be incorrect.)

A function I use a lot is this table randomizer, takes a table as input and returns the same table but the contents inside of it randomized.

function RandomizeTable(tbl)
local newt = {}
for i = 1, #tbl do
local n = math.random(1, #tbl)
table.insert(newt, tbl[n])
table.remove(tbl, n)
end
return newt
end

It’s great that you’re doing this! I don’t know if your module already has the following functions, but one that I know is used a lot is to shorten a number by using SI prefix symbols along with one that keeps a set number of decimals.

Here are some other simple functions that I have in my own module:

  • Convert dictionary to array
  • Return the object with the specified value at the designated key in an array of objects (dictionaries)