How could I use math.clamp with an attribute?

Hello, I am making a stamina system and at the moment, it does work, but I do not know how I could use math.clamp with an attribute to stop the stamina going below 0 or above the max stamina.

I did find a post about this but in that case it did not help for which I was trying to do.

Just use math.clamp whenever Instance.AttributeChanged is fired. You can also use Instance:GetAttributeChangedSignal

2 Likes

I’ll go test this out, see what I can do about that.

This works, but is there a cleaner way to do this or no?

char:GetAttributeChangedSignal("Stamina"):Connect(function()
	char:SetAttribute("Stamina", math.clamp(char:GetAttribute("Stamina"), 0, char:GetAttribute("MaxStamina")))
end)
1 Like

Unfortunately not, but I agree, attributes should support combined read & write operations, i.e; append, increment (and decrement via negative increments).

Object:SetAttribute("Attr", "Hello")
Object:AppendAttribute("Attr", " world!") --'Append' would be used to concatenate attributes.
--Hello world!
Object:SetAttribute("Num", 10)
Object:IncrementAttribute("Num", 10) --'Increment' would be used to increment (and decrement) numeric attributes.
--20

Perhaps you could make a feature request.

1 Like