hi all.
TLDR = read the last codeblock
i have been working on a framework combining OOP ish framework and attributes.
i call this system Attribute Oriented Programming (henceforth named AOP).
what do i want to achieve:
to best explain what i want to achieve i am going to explain how i want to set up a class.
1. create a module script
2. the name of the module script will be the name of the class(rename the module script).
3. Add the “AOP” Tag.
The AOP tag shows that it is managed by the AOP Framework.
This will add an Attribute called Class

if the Class attribute is set to “Nil” it means there is no class connected to this instance
4. Change Class to Class
to show AOP this module script is a class script change the Atribute “Class” to “Class”

AOP will not set up the module script as a class
5. Script the class
The code inside the module script will look something like this
it returns a dictionary.
The index will be the name of the attribute.
there are expections:
- If there is a property with the name … it will react to the property
- If there is a roblox scripting signal with this name it will connect to the roblox scripting signal
- If the name is ClickDetector it will create a click detector
- If the name is RemoteEvent it will connect trough AOP Remote event
- If the name is RemoteFunction it will connect trough AOP RemoteFunction
- If the index is a number it will treat it as an inherentance… this will be explained later
(i might add more later)
each index has a table with the following:
[“Client”] = should this code be running in the client enviroment localy (including studio test)
[“Server”] = should this code be running on the server (including studio test)
[“Studio”] = should this code be running in edit mode in studio (Exlcuding. studio test)
[“Default Value”] = if it is an attribute this is the value being set if the instance doenst have an attribute. this will be ignored on clickdetector, remoteevent, remotefunction, ScriptingSignal
[“Validation”] = this is a validation function, I will explain here how it works, why the validation is necessary is explained in a later part.
with validation you can do 3 things:
- Return nil => this means that the new value is accepted
- Return … => if this returns anything… it will execute the function with the new value
it will alsow change the attribute to the new value - error => if this errors it will set back the old value and prints an error message. this will not execute the function.
[“Function”] = this is the main part, it has 3 varaibles
- the instance on wich the property is changed
- the new value (keep in mind this is after validation, and validation does not error)
- the old value (what the value was before it changed)
this is the function to be executed when the Atribute is changed
There are some functions in AOP to help you out.
- GetAllChildrenOfClass
- GetAllDecendentsOfClass
- GetAllOfClass
- GetFirstAncestorOfClass
- GetfirstAncestorWithAttribute
- RemoteEvent
- Remotefunction
You can see them all in the code I added
the scripting is now all done.
if you want you can add an instance to the module script without the AOP tag.this instance will be cloned when using AOP.New(Classname:String,instance:Instance [optional])
if u use a script to create new classes, this instance will be cloned.
- Create the instance
to create a new instance of that class you need to do the following
- Create an instance
- add the AOP tag
- Change atribute Class to the new classname
it will set all atributes for u and put in the default values.
if the atribute already exist and the new value passes validation it will keep that.
all atributes that are not in the new class will be removed.
in the image below i made an Example class. added 2 atributes and created a part that has this class linked.
here you can see:
1: the example part
2: the script used in the class
3: the attibutes that get automaticly added
so far i hope this is understandable and at this point i realy want to get to the issue. however there are 2 things i promised to explain first, Inheretance and validation
Inheritance
first some termenology
ChildClass = is the class the current class is based upon
overlaid = taking a childclass. then comparing a new class. if there is conflicting data the new class will be chosen as main data
current class = the class you are currently working on
Inherentance is done in order. meaning the first ChildClass is copied and then the 2nd class is overlaid. the order is the index number in the returned table.
Let’s say the child class has an attribute called “ThemeColor” and the current class also has an attribute called “ThemeColor”. It will take the attribute of the current class or latest class.
this will be done untill all inheretances is completed. and finally overlay the current class.
Validation
why is validation important.
Let’s take the same class and a attribute called “ThemeColor”
you create a function you want to run only on the server
and now you create an instance with that class.
the main function will not run in studio , only during playtest and playing the game.
however if you change this attribute in studio it will still validate the value.
That way if the game starts you can rest ashured every class has a proper value
this is particularly useful when someone else is helping you create a world. as you can still check their input.
The Code for AOP and some explination
the code has 3 main sections
1: AOP => all public functions
2: AOPManager => bits of code that are used in multiple parts, so I don’t have to write them out over and over again (DRY principle)
3: ClassManager => manages all classes and childclasses
4:Instancemanager => handles all instances that arent classes
local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local HttpService = game:GetService("HttpService") -- for turning tables into strings
local AOP = {}
local AOPManager = {}
local ClassManager = {}
local InstanceManager = {}
--[[ https://patorjk.com/software/taag/ -> ansi regulair
█████ ██████ ██████
██ ██ ██ ██ ██ ██
███████ ██ ██ ██████
██ ██ ██ ██ ██
██ ██ ██████ ██
All public functions
]]
function AOP.GetAllChildrenOfClass (Instance:Instance,ClassName:string):SharedTable
local Toreturn = {}
for i,v in Instance:GetChildren() do
if v:GetAttribute("_Class") == ClassName then
table.insert(Toreturn,v)
end
end
return Toreturn
end
function AOP.GetAllDecendentsOfClass (instance:Instance,ClassName:string):SharedTable
local Toreturn = {}
for i,v in instance:GetDescendants() do
if v:GetAttribute("_Class") == ClassName then
table.insert(Toreturn,v)
end
end
return Toreturn
end
function AOP.GetAllOfClass (ClassName:string):SharedTable
local returntable = {}
for i,v in pairs(CollectionService:GetTagged("AOP")) do
if v:GetAttribute("Class") == ClassName then
table.insert(returntable,v)
end
end
return returntable
end
function AOP.GetFirstAncestorOfClass(instance:Instance,ClassName:String):Instance
while instance ~= game do
instance=instance.Parent
if instance:GetAttribute("_Class") == ClassName then
return instance
end
end
end
function AOP.GetfirstAncestorWithAttribute(instance:Instance,AttributeName:string):Instance
while instance ~= game do
instance=instance.Parent
if instance:GetAttribute(AttributeName) ~= nil then
return instance
end
end
end
function AOP.PrintAllClassNames()
local returntable = {}
for i,v in ClassManager.FullClassData do
table.insert(returntable,i)
end
return returntable
end
function AOP.Set(instance,PropertyName,Value)
local connectiontype = AOPManager.connectiontype(instance,PropertyName)
if connectiontype == "Property" then
instance[PropertyName] = Value
elseif connectiontype == "Attribute" then
instance:SetAttribute(PropertyName,Value)
end
return instance
end
function AOP.Get(instance,PropertyName)
local connectiontype = AOPManager.connectiontype(instance,PropertyName)
if connectiontype == "Property" then
return instance[PropertyName]
elseif connectiontype == "Attribute" then
instance:GetAttribute(PropertyName)
end
return instance
end
local Instanceadded :RBXScriptConnection = nil
local InstanceRemoved :RBXScriptConnection = nil
function AOP.Enable()
-- step 1 do all classes
for _,instance in CollectionService:GetTagged("AOP") do
if instance:GetAttribute("Class") == "Class"then
InstanceManager.SetupIntance(instance)
end
end
warn("classsetup completed")
print(ClassManager)
-- step 2 do all instances
for _,instance in CollectionService:GetTagged("AOP") do
if instance:GetAttribute("Class") ~= "Class"then
InstanceManager.SetupIntance(instance)
end
end
warn("instancesetup completed")
print(InstanceManager)
-- step 3 lissen for either
Instanceadded = CollectionService:GetInstanceAddedSignal("AOP"):Connect(function(instance)
print("Instance found: "..instance.Name)
InstanceManager.SetupIntance(instance)
end)
InstanceRemoved = CollectionService:GetInstanceRemovedSignal("AOP"):Connect(function(instance)
end)
end
function AOP.Disable()
Instanceadded:Disconnect()
InstanceRemoved:Disconnect()
InstanceManager.Disable()
ClassManager.Disable()
end
if AOP.Runcontent == "Studio" then
AOP.RemoteEvent = Instance.new("RemoteEvent")
AOP.RemoteEvent.Parent = script
AOP.RemoteFunction = Instance.new("RemoteFunction")
AOP.RemoteFunction.Parent = script
elseif AOP.Runcontent == "Client" then
AOP.RemoteEvent = script:WaitForChild("RemoteEvent")
AOP.RemoteFunction =script:WaitForChild("RemoteFunction")
end
local Server = RunService:IsServer()
local Client = RunService:IsClient()
if Server and Client then
AOP.Runcontent = "Studio"
elseif Server then
AOP.Runcontent = "Server"
elseif Client then
AOP.Runcontent = "Client"
end
--[[
█████ ██████ ██████ ███ ███ █████ ███ ██ █████ ██████ ███████ ██████
██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██████ ██ ████ ██ ███████ ██ ██ ██ ███████ ██ ███ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██████ ███████ ██ ██
AOPManager houses all internal functions invisible for the end user
]]
do
function AOPManager.tostring(...)
local returntable = {}
for i,v in {...} do
if typeof(v) == "table" then
table.insert(returntable,HttpService:JSONEncode(v))
else
table.insert(returntable,tostring(v))
end
end
return returntable
end
function AOPManager.print (...)
if script:GetAttribute("Silence") == false then
local toprint = "AOP"
for i,v in AOPManager.tostring(...) do
toprint = toprint..": "..v
end
print(toprint)
end
end
function AOPManager.warn (...)
if script:GetAttribute("Silence") == false then
local toprint = "AOP"
for i,v in AOPManager.tostring(...) do
toprint = toprint..": "..v
end
warn(toprint)
end
end
function AOPManager.Error (msg)
if script:GetAttribute("Silence_Errors") == false then
error(msg,2)
else
task.cancel(coroutine.running())
end
end
function AOPManager.DeepClone(original)
local clone = table.clone(original)
for key, value in original do
if type(value) == "table" then
clone[key] = AOPManager.DeepClone(value)
end
end
return clone
end
function AOPManager.TableAdd(tablename,parent,value,key)
if parent[tablename] == nil then
parent[tablename] = {}
if key == nil then
table.insert(parent[tablename],value)
else
parent[tablename][key] = value
end
end
end
function AOPManager.connectiontype(instance, Name)
-- Check for known instance types first
if Name == "ClickDetector" then
return "ClickDetector"
end
if Name == "RemoteEvent" then
return "RemoteEvent"
end
if Name == "RemoteFunction" then
return "RemoteFunction"
end
-- Try to access the property and determine its type
local success, result = pcall(function()
return typeof(instance[Name])
end)
if success and result ~= nil then
return result
else
-- If property doesn't exist or can't be accessed, treat as attribute
return "Attribute"
end
end
local print = AOPManager.print
local warn = AOPManager.warn
local error = AOPManager.Error
end
--[[
██████ ██ █████ ███████ ███████ ███ ███ █████ ███ ██ █████ ██████ ███████ ██████
██ ██ ██ ██ ██ ██ ████ ████ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ███████ ███████ ██ ████ ██ ███████ ██ ██ ██ ███████ ██ ███ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ███████ ██ ██ ███████ ███████ ██ ██ ██ ██ ██ ████ ██ ██ ██████ ███████ ██ ██
everything related to managing classes
]]
do
--ClassManager.RawClassData[classname] = {rawclassdata}
ClassManager.RawClassData = {}
--ClassManager.FullCLassDataa[classname] = {rawclassdata}
ClassManager.ClassData = {
["nil"] = {
["Functions"] = {};};
["Class"] = {}
}
--ClassManager.waitingforinheritenca[classname] = {strings with classes}
ClassManager.PendingClasses = {}
function ClassManager.GetUnresolvedDependencies(classname, Classdata)
local Pendencies = {}
for _, v in ipairs(Classdata) do
if ClassManager.FullClassData[v] == nil then
table.insert(Pendencies, v)
end
end
if #Pendencies == 0 then
return nil
else
return Pendencies
end
end
function ClassManager.ResolvePendingInheritance()
local stillPending = {}
for _, className in ipairs(ClassManager.PendingClasses) do
local rawClassData = ClassManager.RawClassData[className]
local unresolved = ClassManager.GetUnresolvedDependencies(className, rawClassData)
if not unresolved then
ClassManager.SetUp(className, rawClassData)
end
end
end
function ClassManager.SetUp (className:string, RawClassdata, Object)
-- Prevent duplicate setup
if ClassManager.FullClassData[className] ~= nil then
print("Class",className,"Already exist")
return
end
-- Store raw inheritance data
ClassManager.RawClassData[className] = RawClassdata
-- Check for unresolved dependencies
local unresolved = ClassManager.GetUnresolvedDependencies(className, RawClassdata)
if unresolved then
print("Class:", className, "waiting for inheritance:", HttpService:JSONEncode(unresolved))
table.insert(ClassManager.PendingClasses, className)
return
end
print("class",className,"Ready for setup")
local FullClassData = {
["Parents"] = {};
["Children"] = {};
["Functions"] = {};
["Object"]=Object;
}
local ClassFunctions = FullClassData["Functions"]
-- Set up parent relationships
for _,ParentClassName in ipairs(RawClassdata) do
local ParentData = ClassManager.FullClassData[ParentClassName]
table.insert(FullClassData["Parents"],ParentClassName)
table.insert(ParentData["Children"],className)
for ii,vv in ParentData["Functions"] do
if vv[AOP.Runcontent] == true then
ClassFunctions[ii] = AOPManager.DeepClone(vv)
else
if AOP.Runcontent == "Studio" and vv["Validation"] then
ClassFunctions[ii] = {
["DefaultValue"] = vv["DefaultValue"];
["Validation"] = vv["Validation"];
}
end
end
end
if ParentData["Object"] and FullClassData["Object"] == nil then
FullClassData["Object"] = ParentData["Object"]
end
end
-- set up the new class data
for i,v in RawClassdata do
if type(i) ~= "number" then
if v[AOP.Runcontent] == true then
FullClassData[i] = AOPManager.DeepClone(v)
end
else
if AOP.Runcontent == "Server" and v["Validation"] then
FullClassData[i] = {
["DefaultValue"] = v["DefaultValue"];
["Validation"] = v["Validation"];
}
end
end
end
ClassManager.FullClassData[className] = FullClassData
print("class",className,"Setup completed",HttpService:JSONEncode(FullClassData))
return ClassManager.FullClassData[className]
end
function ClassManager.Delete(className:string)
InstanceManager.DisconnectAllOfClass(className)
local FullClassData= ClassManager.FullClassData[className]
for i,v in FullClassData["Children"] do
ClassManager.Delete(v)
end
ClassManager.RawClassData[className] = nil
ClassManager.FullClassData[className] = nil
ClassManager.PendingClasses[className] = nil
end
end
function ClassManager.SetupFromModulescript(Modulescript:ModuleScript)
local classname = Modulescript.Name
local RawClassData = require(Modulescript)
ClassManager.SetUp(classname,RawClassData,Modulescript:GetChildren()[1])
end
--[[
██ ███ ██ ███████ ████████ █████ ███ ██ ██████ ███████ ███ ███ █████ ███ ██ █████ ██████ ███████ ██████
██ ████ ██ ██ ██ ██ ██ ████ ██ ██ ██ ████ ████ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ███████ ██ ███████ ██ ██ ██ ██ █████ ██ ████ ██ ███████ ██ ██ ██ ███████ ██ ███ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ████ ███████ ██ ██ ██ ██ ████ ██████ ███████ ██ ██ ██ ██ ██ ████ ██ ██ ██████ ███████ ██ ██
everything related to instances
]]
do
InstanceManager["Instances"] = {}
function InstanceManager.Connect(instance:Instance,Name:string,ConnectionData):RBXScriptConnection
print(instance,Name,ConnectionData)
local connection = nil
local connectionType = AOPManager.connectiontype(instance,Name)
local Validation = ConnectionData["Validation"]
local func = ConnectionData["Function"]
if connectionType == "RBXScriptSignal" then -- set as scripting signal
connection = instance[Name]:Connect(function()
func(instance)
end)
elseif connectionType == "property" then -- set as property
local oldvalue = nil
oldvalue = instance[Name]
connection = instance:GetPropertyChangedSignal(Name):Connect(function()
func(instance,instance[Name],oldvalue,instance:GetAttributes())
oldvalue = instance[Name]
end)
elseif connectionType == "Attribute" then -- set as attribute
local oldvalue = instance:GetAttribute(Name)
local debounce = true
connection = instance:GetAttributeChangedSignal(Name):Connect(function()
if debounce == true then
debounce = false
local newvalue = instance:GetAttribute(Name)
local err , response = xpcall(function()
if Validation ~= nil then
local validationvalue = Validation(newvalue)
if validationvalue ~= nil then
newvalue= validationvalue
end
end
return func(instance,newvalue,oldvalue,instance:GetAttributes())
end,
function(error)
warn(error)
print(debug.traceback())
end
)
if err == false then
instance:SetAttribute(Name,oldvalue)
warn(response)
else
if response == nil then
oldvalue = newvalue
else
instance:SetAttribute(Name,response)
oldvalue = response
end
end
task.wait()
debounce = true
end
end)
elseif connectionType == "ClickDetector" then -- set as clickdetector
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = instance
connection = ClickDetector.MouseClick:Connect(function(player)
func(instance,player)
end)
elseif connectionType == "RemoteEvent" then
elseif connectionType == "RemoteFunction" then
end
if InstanceManager["Instances"][instance] == nil then
InstanceManager["Instances"][instance] = {}
end
InstanceManager["Instances"][instance][Name] = connection
end
function InstanceManager.Disconnect(instance:Instance,Name:string)
if Name then
if InstanceManager["Instances"][instance][Name] then
InstanceManager["Instances"][instance][Name]:Disconnect()
InstanceManager["Instances"][instance][Name] = nil
end
else
for i,v in InstanceManager["Instances"][instance] do
v:Disconnect()
end
InstanceManager["Instances"][instance] = nil
end
end
function InstanceManager.SetupIntance(instance:Instance)
local class = instance:GetAttribute("Class")
warn(instance,class)
instance:SetAttribute("Class",nil)
InstanceManager.Connect(instance,"Class",{
["Server"] = true;
["Client"] = true;
["Studio"] = true;
["DefaultValue"] = 0;
["Function"] = function(instance,Newvalue)
print(instance,Newvalue)
if Newvalue ~= nil then
if InstanceManager["Instances"][instance] then
for Name,connection in InstanceManager["Instances"][instance] do
if Name ~= "Class" then
connection:Disconnect()
end
end
end
end
for name,Data in ClassManager.ClassData[Newvalue] do
InstanceManager.Connect(instance,name,Data)
end
for name,value in instance:GetAttributes() do
if ClassManager.ClassData[Newvalue][value] == nil then
instance:SetAttribute(name,nil)
end
end
end,
["Validation"] = function(Instance,NewValue)
if NewValue == nil then return "nil" end
if ClassManager.ClassData[NewValue] ~= nil then return end
error("Class Not found")
end,
})
task.wait()
instance:SetAttribute("Class",class)
end
end
warn(AOP)
return AOP
--[[
the issues:
if you have read all above let me first say thank you. i know that my many spelling mistakes and wording makes it realy hard to get past any piece of text i have written.
here is a list of the issues i come across and therefore I don't know how to continue
1: readability.
for me this is a large piece of code. and i often loose track of where i am, or what changes i have made.
i do want to keep it in 1 module and that module runs both on studio, server and client. but how do i keep writing code that isnt intuitive. and hard to read. how do i make it more intuitive or cohrent with scripting rules?
2: versioning.
i have no idea how to version this code, fork it and merge it within roblox framework.
and mostly create a sidepath for testing.
3: studio issues:
in test mode and play mode all classes will not change.
in studio however you can edit the module scripts.
how do i write it in a dynamic way... do i need to reload the entire class every time a change is made, or is there a better way. how do i detect when a change is made?
or is it better to update all the classes on close.
4: inheritance issues.
what to do if an inheritance disappears mid-session, do I disconnect all classes that are dependent on it? Or just continue with old data?
5: strucutre wise.
do i make an actual nill and Class class, or do I hardcode them in the system
i think my main question would be: If u were doing something like this. how would you do it?
]]

