How to do math on vector3 attribute

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? A way to do math on an vector 3 attribute.

  2. What is the issue? It doesn’t add.

local rotation = script.Parent:GetAttribute("RotationBuilding")

rotation:SetAttribute(rotation + Vector3.new(0, 45, 0)) -- Doesnt work

local newRotation = rotation + Vector3.new(0, 45, 0) -- Doesnt work
script.Parent:SetAttribute("RotationBuilding", rotation + Vector3.new(0, 45, 0))
3 Likes

You’re forgetting the name of the attribute itself:

script.Parent:SetAttribute("RotationBuilding", rotation + Vector3.new(0, 45, 0))

Also Vector3 values do not have the :SetAttribute() function, only Instances do!

When starting out it helps to use variables to reference everything you use more than once. E.g.:

local BUILDING_ROTATION_ATTR = "RotationBuilding"
local building = script.Parent
--Increment building's rotation by 45 degrees
local rotation = building:GetAttribute(BUILDING_ROTATION_ATTR)
rotation += Vector3.new(0, 45, 0)
building:SetAttribute(BUILDING_ROTATION_ATTR, rotation)

You could also write a reusable function if you need this kind of code in many places:

function incrementAttr(instance: Instance, attributeName: string, incrementAmount: number): number
    local oldValue = instance:GetAttribute(attributeName)
    local newValue = oldValue + incrementAmount
    instance:SetAttribute(attributeName, newValue)
    return newValue --Might as well return the new value, but it's not really needed.
end

function incrementBuildingRotation(building: Model, incrementAmount: number): number
    return incrementAttr(building, "RotationBuilding", incrementAmount)
end

--Example
incrementBuildingRotation(building, 45)