Add UpdateAttribute and IncrementAttribute methods to instances

As a Roblox developer, it is currently too hard to change an attribute’s value based upon its current value.


I commonly find myself doing this to a similar extent:

local value = workspace:GetAttribute("example");
workspace:SetAttribute("example", not value);

Which is overly lengthy for such a simple action.

My proposition is that we get an ‘UpdateAttribute’ method which acts just like the UpdateAsync method of datastores (taking a function that returns the new value). For example:

workspace:UpdateAttribute("example", function(value)
    return (not value);
end)

Though I could create a wrapper function to this, shouldn’t it just be a native feature?


Once more, I’d like to see a ‘IncrementAttribute’ method which takes an incrementor - this’d make attributes that move in incremental steps much easier to add. For example:

workspace:IncrementAttribute("players_joined", 1);

If Roblox is able to address this issue, it would improve my development experience because I wouldn’t have to go through the rigmorale of getting and setting an attributes value, it’d make attributes far easier to utilise.

42 Likes

Was needing this yesterday. The biggest use case for me for increment attribute is for weapons, each weapon will have an ammo attribute, and it would be easier to just do weapon:IncrementAttribute("Ammo", -1).

I personally haven’t needed an update attribute, but I see the use case for it.

4 Likes

Hi! :wave:

I have also encounter this so it’s a nice suggestion, but for now I’ll be adding this to AttributeService.

2 Likes

I would use IncrementAttribute a lot. I find myself doing things like

player:SetAttribute("Money", player:GetAttribute("Money") + 10)

which could be replaced with

player:IncrementAttribute("Money", 10)

Here is something that I just wrote that could be shortened by having IncrementAttribute:

local locationFolder = getLocationFolderFromObject(object)
locationFolder:SetAttribute("ObjectsMined", locationFolder:GetAttribute("ObjectsMined") + 1)

becomes

getLocationFolderFromObject(object):IncrementAttribute("ObjectsMined", 1)
5 Likes

Bumping to say that this is heavily needed!

Would love IncrementAttribute, but I can see some cases where UpdateAttribute would be nice! This is the cleanest I could get my code.

for Place, PlayerInfo in VoteArray do
	local Player = PlayerInfo.Player
	local Currencies = Player.Currencies
	
	Currencies:SetAttribute("BrickCoins", Currencies:GetAttribute("BrickCoins") + Rewards[Place])
	
	if Place == 1 then
		Player.PlayerData:SetAttribute("MultiplayerWins", Player.PlayerData:GetAttribute("MultiplayerWins") + 1)
	end
end
1 Like

This would be great, honestly the lack of these features is probably keeping a lot of people from using attributes because of how ugly their code pattern looks.

1 Like

Yes this could be helpful, there are ways around it such as using a function that increments the attribute instead of you having to do it yourself