In this post, I would like to present the most simple attribute module that I’ve made. This module is aimed for efficient and simple code. I will provide 2 code examples showing it’s main features:
table-like interface
local Attribute = require(path.to.module)
local player = game.Players.LocalPlayer
local myAttributes = Attribute(player)
myAttributes.Money = 500
print(myAttributes.Money)
Javascript-like functions
The main difference between this module and any alternative is the option to chain functions, inspired by many node js modules. This makes it easier to set item attributes only once in a script.
local Attribute = require(path.to.module)
-- ...
for _, limbName in limbs do
local limb = character[limbName]
Attribute(limb):Set('LimbHealth', 100):Set('Attached', true)
end
Attrbute.rbxm (1018 Bytes)
Code
-- dauser
local module = {}
module.__index = function(self, key)
if rawget(module, key) then
return rawget(module, key)
else
return self._instance:GetAttribute(key)
end
end
module.__newindex = function(self, key, value)
return self._instance:SetAttribute(key, value)
end
function module:Get(key)
return self._instance:GetAttribute(key)
end
function module:Set(key, value)
self._instance:SetAttribute(key, value)
return self
end
export type AttributeMaster = {
Get: (self, key: string) -> (),
Set: (self, key: string, value: number | string | boolean | Instance) -> (AttributeMaster)
}
return function(instance: Instance): AttributeMaster
local self = {}
self._instance = instance
setmetatable(self, module)
return self
end