 # What is the most efficient way to perform arithmetic on attributes?

So currently if you try to perform arithmetic on attributes like this:

``````local part = Instance.new("Part")
part.Parent = workspace

part:SetAttribute("test",1) -- create an attribute called 'test' and give it a value of '1'

part:GetAttribute("test") += 1 -- that will cause an error
``````

It will cause an error because you can’t perform arithmetic on attributes like that so I was confused about this because I didn’t know how to perform arithmetic on attributes and in the end I found a solution but I don’t know if it’s the most efficient one:

``````local part = Instance.new("Part")
part.Parent = workspace

part:SetAttribute("test",1) -- create an attribute called 'test' and give it a value of '1'

print(part:GetAttribute("test")) -- prints '1'

local test = part:GetAttribute("test") -- store the 'test' attribute value as a variable so we can perform arithmetic on it later
test += 1

part:SetAttribute("test",test)

print(part:GetAttribute("test")) -- prints '2'
``````

That code above is currently my best solution to the problem but I would like to know if it’s the most efficient one or if this could be done in a more efficient way?

im not sure if you count this is efficient

``````Instance:SetAttribute("Attribute",(
-- Arithmetic
math.cos(tick * 10) *
(n^ 1.3)
))
``````

The way you have it should be pretty quick

Let me explain why it errors.
The += is just context sugar for: thing = thing + 1
The :GetAttribute(“key”) returns the value stored by the key, just like a table:

``````local myTable = {}
myTable["key"] = "value"
print(myTable["key"], type(myTable["key"])) --prints: value string

--now look at this:
local part = Instance.new("Part",workspace)
part:SetAttribute("key","value")
print(part:GetAttribute("key"), type(part:GetAttribute("key"))) --prints: value string
``````

Basically, Attributes are just a more restrictive table that instances can have!

So your problem:
`part:GetAttribute("test")` evaluates to 1 so you are in effect saying: `1 += 1`

The solution to your woes would be this:

``````part:SetAttribute("test", part:GetAttribute("test") + 1)
``````

GetA “test” from part returns 1, 1+1 is 2, SetA “test” on part to be 2.