I’ve made a similar post a couple months ago and wondering if there is room for improvement but I’ve never gotten a response. I’m trying once again.
local Signal = require(script.Signal)
local Noob = {}
local mt = {}
mt._events = {}
function Noob.new(health: number?, name: string?)
local properties = {
Health = health or 100,
Name = name or "Bob"
}
for property, value in pairs(properties) do
mt._events[property] = Signal.new()
end
mt.__newindex = function(t, k, v)
--print("User has indexed " .. k .. " with " .. v .. " | __newindex has been invoked")
if properties[k] then
rawset(properties, k, v)
if mt._events[k] then -- if a signal exists for that property
mt._events[k]:Fire(v)
end
else
warn("[NoobClass] You cannot set new values into this Class")
end
end
mt.__index = function(t, k)
--print("User has indexed " .. k .. " | __index has been invoked")
local property = rawget(properties, k)
if property then return property end
end
mt.__tostring = function(t)
return "NoobClass"
end
return setmetatable(Noob, mt)
end
function Noob:GetPropertyChangedSignal(property, listener)
local signal = mt._events[property]
if signal then return signal end
end
return Noob