How to Smoothly change Attribute values like Tween

I want to tween attributes, I tried this

local Tween = game.TweenService
local part = workspace.Part
Tween:Create(part, TweenInfo.new(1), {AttributeSample = 50}):Play()

but the problem is that there is no Property called “AttributeSample” because it is an attribute.

I could use a for loop, but I want it to be smooth. Is there a Service to ‘tween’ Attributes, or a method?

I believe you can just use the following methods to tween attributes like CFrame variables, it may not be that neat but it should work:

1 Like

You could use RunService to make a smooth transition using stepped on the server or render stepped on the client

This is a case where you should consider using a ValueObject instead of an attribute. You could hypothetically “tween an attribute” by using a ValueObject as a proxy like dthecoolest’s post mentions but at that point using attributes is pointless because you already have a ValueObject to work with. Same manner of replication, just how you’re accessing your number will change from GetAttribute to the value of your ValueObject.

1 Like

this is an alternative for my case, but I am just asking if there is a possible way to ‘tween’ Attributes

Otherwise, directly, no, you can’t.

1 Like

You can’t directly tween an attribute - but you can tween the value base property of a Value object. Assuming your attribute is an integer (make minor changes if otherwise), the code below subtly utilises Value bases to tween attributes.

local part = game.Workspace.MyBrick 
--Change this to the object with the attributes!
local info = TweenInfo.new()
--Change this to match the kind of tween you want!
local target = 10
--Change this to the destination value you want!
local attName = "MyValue"
--Change this to the name of your attribute!

local ts = game:GetService("TweenService")
local run = game:GetService("RunService")
local value = Instance.new("IntValue",part)
value.Value = part:GetAttribute(attName)

local tween = ts:Create(value,info,{Value=target})
run:BindToRenderStep("ValueFollow",1,function()
   part:SetAttribute(attName,value.Value)
)

tween:Play()
tween.Completed:Wait()
run:UnbindFromRenderStep("ValueFollow")
value:Destroy()

Hope it works!

3 Likes