Attributor
Wraps an Instance so its attributes read and write like plain table fields. attributor.Key
calls instance:GetAttribute("Key"), attributor.Key = value calls
instance:SetAttribute("Key", value), and calling the object (attributor()) returns a
dictionary snapshot of every attribute. Nothing is cached; every access goes straight to the
instance, so values are always current and writes replicate exactly like normal attributes.
local Attributor = require(path.to.Attributor)
local stats = Attributor(workspace.Dummy)
stats.Health = 100 -- workspace.Dummy:SetAttribute("Health", 100)
stats.Health -= 25 -- reads, then writes 75
print(stats.Health) -- 75
print(stats.MissingKey) -- nil, the attribute does not exist
for name, value in stats() do
print(name, value) -- every attribute on the instance
end
stats:Destroy() -- optional; also happens when the instance is destroyed
Instance and Destroy are real fields on the object, so attributes with those two names cannot
be reached through the proxy. Writes are subject to the usual SetAttribute rules (attribute
names must be valid and values must be attribute-compatible types) and will error otherwise.
Credits: written by KashTheKing. No third-party dependencies.
Installation and guide: Attributor package page.
Properties
Instance
This item is read only and cannot be modified. Read OnlyAttributor.Instance: InstanceThe wrapped instance. Becomes nil once the Attributor is destroyed.
Functions
new
Attributor.new() → Attributor--
A proxy where proxy.Name gets and proxy.Name = value sets the attribute Name.
Creates an attribute proxy for instance. The module itself is this function, so call it as
Attributor(instance). The proxy connects to instance.Destroying and cleans itself up when the
instance is destroyed, so calling Attributor:Destroy yourself is only needed when you
want to drop the wrapper earlier than that.
Destroy
Attributor:Destroy() → ()
Detaches the proxy from its instance: clears Attributor.Instance so the
instance can be garbage collected and removes the metatable, so later reads return nil
and later writes only touch the now-plain table. Does not destroy or modify the instance.
Safe to call more than once; also runs automatically when the instance is destroyed.