Function making Attributes simple and easy!

I made a function making getting or computing attributes easier!
Just paste it on your script!

function at(o,a)
	local Attribute = setmetatable(
		{},
		{__call = function(t,n) if n == nil then return o:GetAttribute(a)
			else o:SetAttribute(a,n) end end;
		__unm = function(t) return -o:GetAttribute(a) end;
		__add = function(t,v) 
			if typeof(t) == "number" then return t+o:GetAttribute(a) 
			else return o:GetAttribute(a)+v end end;
		__sub = function(t,v) 
			if typeof(t) == "number" then return t-o:GetAttribute(a) 
			else return o:GetAttribute(a)-v end end;
		__mul = function(t,v) 
			if typeof(t) == "number" then return t*o:GetAttribute(a) 
			else return o:GetAttribute(a)*v end end;
		__div = function(t,v) 
			if typeof(t) == "number" then return t/o:GetAttribute(a) 
			else return o:GetAttribute(a)/v end end;
		__mod = function(t,v) 
			if typeof(t) == "number" then return t%o:GetAttribute(a) 
			else return o:GetAttribute(a)%v end end;
		__pow = function(t,v) 
			if typeof(t) == "number" then return t^o:GetAttribute(a) 
			else return o:GetAttribute(a)^v end end;
		})

	return Attribute
end

For example:

game.ReplicatedStorage.GameValues:SetAttribute("Time",120)
local Time= at(game.ReplicatedStorage.GameValues,"Time")
print(Time()) --> 120
print(Time+120) --> 240
print(Time/24) --> 5
print(23480+Time) --> 23600
print(Time() == 120) --> true
Time(60) --Set 'Time' attribute to 60
print(Time+23) --> 83
Time(Time+50) --> Add 50 to 'Time' attribute
print(Time()) --> 110

If there are problems which are bad, reply!

4 Likes

A good development utility, liked it.

If anything I think this makes working with attributes unnecessarily complicated, especially when the code isn’t exactly readable due to the strange choice in indenting/alignment and few-letter variable names. At this point you’re probably looking to use a ModuleScript to store your data instead, or just clearly writing out your computations between two number attributes.

2 Likes