Some new functions I made

Hey there! I made some new functions that could help someone save their precious time so they don’t have to type a lot of stuff!
I will explain how this works in case you’re wondering. If you think this should go in another place, please tell me so I can move it to that category.
MainModule.lua (1.5 KB)
Updated Version here:
Get it here or More Functions - Roblox
First insert a script, and require it.

local module = require(5323642034)

Now we can start using the functions!

Function 1

module:GetChildrenOfClass(object, className)
This function returns an array of children which has that class.
Example:

for i, v in pairs (module:GetChildrenOfClass(workspace, 'BasePart')) do
print(v.Name)
end

Only children of workspace that has the class BasePart will be printed. Stuff like Folders or Terrain don’t have this class so their name won’t be printed.

Function 2

module:GetChildrenOfProperty(object, propertyName)
This function returns an array of children which has that property.
Example:

for i, v in pairs (module:GetChildrenOfProperty(workspace, 'WaterWaveSize')) do
print(v.Name)
end

Only Terrain will be printed because only Terrain has this property. If you put Anchored then only objects with the property Anchored will be printed.

Function 3

module:GetChildrenWhichHasAChildrenOfClass(object, className)
This function returns an array of children which has a children that has that class. Kind of confusing but you’ll get what I mean.
Example:

for i, v in pairs (module:GetChildrenWhichHasAChildrenOfClass(workspace, 'ClickDetector')) do
print(v.Name)
end

Only objects with a ClickDetector will be printed. If a ClickDetector isn’t in any children of workspace, its Name won’t be printed.

Function 4

module:GetChildrenWhichHasAChildrenOfProperty(object, propertyName)
This function returns an array of children which has a children that has that property.
Example:

for i, v in pairs (module:GetChildrenWhichHasAChildrenOfProperty(workspace, 'CursorIcon')) do
print(v.Name) 
end

Only objects with a ClickDetector will be printed because only ClickDetectors have this property. If a ClickDetector isn’t in any children of workspace, its Name won’t be printed.

Function 5

module:GetAncestors(object)
This function returns an array of ancestors of an object.
Example:

for i, v in pairs (module:GetAncestors(script)) do
print(v.Name)
end

If this script was in folder in ServerScriptService, it would print Folder and ServerScriptService. It won’t print game.

Function 6

module:GetAncestorsOfClass(object, className)
This function returns an array ancestors that has that class.
Example:

for i, v in pairs (module:GetAncestorsOfClass(script, 'Folder')) do
print(v.Name)
end

If this script was in folder in ServerScriptService, it would only print Folder because ServerScriptService isn’t a folder. It won’t print game no matter what.

Function 7

module:GetAncestorsOfProperty(object, propertyName)
This function returns an array of ancestors that has that property.
Example:

for i, v in pairs (module:GetAncestorsOfProperty(script, 'LoadStringEnabled')) do
print(v.Name)
end

If this script was in folder in ServerScriptService, it would only print ServerScriptService because only it has this property. It won’t print game no matter what.

Function 8

module:GetSiblings(object)
This function returns an array of siblings.
Example:

for i, v in pairs (module:GetSiblings(workspace.Folder)) do
print(v.Name)
end

All siblings of Folder will be printed, even Terrain and Camera.

Function 9

module:GetSiblingsOfClass(object, className)
This function returns an array of siblings that has that class.
Example:

for i, v in pairs (module:GetSiblingsOfClass(workspace.Folder, 'Folder')) do
print(v.Name)
end

If there was only one folder in workspace, nothing will be printed. The object itself won’t be printed because we’re finding the siblings not the object.

Function 10

module.tableCombine(table1, table2)
This function combines 2 tables.
Example:

for i, v in pairs (module.tableCombine(workspace:GetChildren(), module:GetAncestors(script))) do
print(v.Name)
end

They would be combined into a table, and it works for :GetChildren(), normal tables and even module functions!

Function 11

module.dictionaryIndex(dictionary, value)
This function finds the value in the dictionary, then returns the index or key.
Example:

local table = {
['Cheese'] = 'Stuff from cows',
['Cows'] = 'Animals'
}
for i, v in pairs (module.dictionaryIndex(table, 'Animals')) do
print(v.Name)
end

Cows would be printed as it is the index of Animals.

Got any questions or recommendations?

If you think I should add more stuff, feel free to tell me so I can add them! I know some of the functions have really long names so suggest some so it’s easier to type out the functions! If you think this isn’t explained clearly please also tell me. You can download the MainModule to check it out!

7 Likes

Do not use the name function, this name is reserved for functions themsleves and using names that are the same as existing statements is bad

It’s just a variable, you can change it. I’m just using it here as an example. But sure, let me change it here so people won’t get confused.

In case anyone wants the source, formatted:

Source

-- Please do not edit anything!

local module = {}
function module.GetChildrenOfClass(object, className)
    local t = {}
    for i, v in pairs (object:GetChildren()) do
        if v:IsA(className) then
            table.insert(t, v)
        end
    end
    return t
end
function module.GetChildrenOfProperty(object, propertyName)
    local t = {}
    for i, v in pairs (object:GetChildren()) do
        local success = pcall(function()
            local p = v[propertyName]
        end)
        if success then
            table.insert(t, v)
        end
    end
    return t
end
function module.GetChildrenWhichHasAChildrenOfClass(object, className)
    local t = {}
    for i, v in pairs (object:GetChildren()) do
        for a, b in pairs (v:GetChildren()) do
            if b:IsA(className) then
                table.insert(t, v)
                break
            end
        end
    end
    return t
end
function module.GetChildrenWhichHasAChildrenOfProperty(object, propertyName)
    local t = {}
    for i, v in pairs (object:GetChildren()) do
        for a, b in pairs (v:GetChildren()) do
            local success = pcall(function()
                local p = b[propertyName]
            end)
            if success then
                table.insert(t, v)
                break
            end
        end
    end
    return t
end
function module.GetAncestors(object)
    local a = object.Parent
    local t = {}
    repeat
        table.insert(t, a)
        a = a.Parent
        wait(.1)
    until a == game
    return t
end
function module.GetAncestorsOfClass(object, className)
    local a = object.Parent
    local t = {}
    repeat
        if a:IsA(className) then
            table.insert(t, a)
        end
        a = a.Parent
        wait(.1)
    until a == game
    return t
end
function module.GetAnscestorsOfProperty(object, propertyName)
    local a = object.Parent
    local t = {}
    repeat
        local success = pcall(function()
            local p = a[propertyName]
        end)
        if success then
            table.insert(t, a)
            break
        end
        a = a.Parent
        wait(.1)
    until a == game
    return t
end
function module.GetSiblings(object)
    local a = object.Parent
    local t = a:GetChildren()
    local h = table.find(t, object)
    table.remove(t, h)
    return t
end
function module.GetSiblingsOfClass(object, className)
    local a = object.Parent
    local t = {}
    for i, v in pairs (a:GetChildren()) do
        if v:IsA(className) then
            table.insert(t, v)
        end
    end
    local h = table.find(t, object)
    if nil then else
        table.remove(t, h)
    end
    return t
end
function module.tableCombine(table1, table2)
    local t = {}
    for i, v in pairs (table1) do
        table.insert(t, v)
    end
    for i, v in pairs (table2) do
        table.insert(t, v)
    end
    return t
end
function module.dictionaryIndex(dictionary, value)
    local index
    for i, v in pairs (dictionary) do
        if v == value then
			    index = i 
			break 
		end
    end
    return index
end
return module


An interesting resource, but

the wait(0.1) often seen within repeat blocks could’ve been reduced to a wait(), to decrease the time for the operation and not cause a timeout.

When working with arrays, using ipairs shows that and it’s a sort of very slight optimization as well.

Let me update it! Thanks!
30 chars

Also it looks weird after I uploaded it. It is formatted in the module script I made. Don’t know why, but it works.

For this, I didn’t know what to name it. All it does is it returns a table of Ancestors of that class.

And yes I will definitely update it or whatever. I guess these stuff I made could save you a few seconds.

For this I’m not too sure what you mean but I think you mean merging dictionaries. I will try to make it and test it out. Cool suggestion!

You should wrap objects the user uses inside of a table or userdata that indexes back to your methods so you have the syntax roblox uses.

workspace.Folder:GetSiblingsOfClass("Folder") is much nicer than module:GetSiblingsOfClass(workspace.Folder, "Folder")

Let me learn it…
30 chars plssssssss

I don’t know if this is possible, as every instance inside of the game would need to be wrapped, AFAIK.

A player doesn’t really need to use CollectionService because they need to create the tag, then give the object a tag. In the end, it only returns a table. CollectionService is useful for multiple things that don’t share the same similarity. These functions use the similarities to make the table.

First of all, collection service is great, but the user would need to set up all the tags beforehand. These functions are just a workaround because Roblox hasn’t implemented stuff like GetChildrenOfClass. And I think you don’t understand this. You call the function, and it returns a table, just like GetChildren. Something like GetAnscestorsOfClass might not be useful in some cases, but I could still make that. I hope you won’t get confused because this is just a workaround for lazy people like me so I can use these in my future projects. I just don’t want to get a plugin, set up all the tags for all the stuff, then run the functions.
Also, if a player doesn’t want to use this module and makes a function himself giving the object a tag, he just has to write the same thing I wrote in the module, with a few changes, and in the end get the same table.