This is my class, i’m trying to make a proxy so that I can detect when a property is changed, however the class is empty unless I create the key after I construct it, such as
Task.new()
Task.Name = "Task"
print(Task.Name) -> "Task"
print(Task.IsCompleted) -> nil
local Task = {}
Task.__index = Task
function Task.new(callback)
local self = setmetatable({}, Task)
-- Properties
self.Name = "Task"
self.IsCompleted = false
self._Callback = callback or nil
self._Objectives = {}
self.Players = {}
-- Events
self.Changed = Signal.new()
self.Completed = Signal.new()
local proxy = setmetatable({}, {
__index = function(_, key)
return self[key]
end,
__newindex = function(_, key, value)
if self[key] ~= value then
self[key] = value
self.Changed:Fire(key, value)
end
end
})
return proxy
end