Is it possible to set the value of an attribute via script?

Hello! I am trying to make my game better for viewing and I have came across an issue.

I am working on a survival game and tools such as axes and pickaxes can be used to damage objects. Therefore, I need to use a value to determine how much health an object is at. However, I want to switch everything to attributes.

While doing some basic testing, I noticed something that I don’t really understand.

local health = script.Parent:GetAttribute("Health")

script.Parent:SetAttribute(health, 50)

Here is a basic script I wrote on a regular part to test. When I run the game, it makes another attribute called “100” and sets the value to 50. I want the attribute called Health to change to 50.

Is this possible?

Script.Parent:SetAttribute("Health", 50)

Try this ?? Might have something to do with ur referencing.

1 Like

See Instance | Roblox Creator Documentation

The “health” is 50 because you returned a number, , so it will make a attribute 50 with the value of 100. Do

script.Parent:SetAttribute("Health", 50)

instead.

2 Likes

SetAttribute() has the parameters Attribute Name and Value, you’re getting the value of the attribute, then setting the attribute named the value instead of the original name.

1 Like

Gotcha, thanks a lot! I’m not too good with attributes… :sweat_smile: