Custom Class Module v1

download | source

This is a module which allows you to define existing objects as classes and set default properties for them and more (see API below)

Documentation

Global functions

  • ClassModule()

    ClassModule(className, searchIn?: Object) -> class

    looks for an object in a specified container
    and caches a custom class, if the searchIn parameter is not specified it will look in the Object_Container defined in the module

  • Class.new()

    Class.new( Name, Parent) -> new instance Class
    cloned to the specified parent

    if no Parent argument is supplied then the Parent will be the DefaultParent defined in the module

Global Methods

  • Class:SetProperties(Properties: dictionary)

    takes a dictionary of properties and values and sets the custom class’ properties so that whenever or from wherever Class.new() is called, the object created retains the custom set properties.

  • Class:DestroyAll()

    destroys all existing instances of the Class.
    StoreObjects (in the module) must be set to true for this to work, enabled by default.

  • Class:GetObjects()

    returns all objects of the Class this function was called on

General Usage


image

pseudo code:



    local CustomClass = require(game.ServerScriptService.ClassCreator)

    local part = CustomClass("part") --> new class called 'part'
    local newPart  = CustomClass("newPart")


    local part1 = part.new("part1") -- part called "part1" is created, parented to workspace by default
    part1.Anchored = true -- index and alter properties as normally

    part.new("part")
    newPart.new("new part", workspace.Model) -- created under workspace.model
  

    local properties =
                       {Anchored = true;
                        Color = Color3.fromRGB(33,255,49);
                        Position = Vector3.new(20,0,40) }
                                      
    part:SetProperties(properties) -- future parts created using part.new() will have the above properties
  
    part.new("green part")  --> this part has all of the properties defined in the above dictionary
 
    wait(10)

    print(#part:GetObjects()) --> 3

    part:DestroyAll() --> all objects of class destroyed
    newPart:DestroyAll() 

This is not limited to parts, you can use your own models etc. too.

This module prevents the need to define objects’ properties separately each time, as :SetProperties() applies all the properties to the part contained, you could use this to set different properties instead of changing the properties each time you clone the instance separately.

You can use Class:DestroyAll() to destroy all objects of a class in the game, for objects created through Class.new().
Require the module on the Client so that an object’s properties are different for every client.


credit to @ReturnedTrue for some suggested functions.

do respond with any suggestions so I can implement them, or bugs if they exist

8 Likes

Helpful, thanks
(30 characters)

1 Like

I’ll consider installing it, thanks!

1 Like

Please choose one

    • This resource was useful to me
    • more features would’ve made it better
    • not useful to me, but good work

0 voters

editing the original post caused the previous one to reset

1 Like