I want to base a parts saturation on a value. The code below does that but I would like to cap the max saturation to the value the part starts with.
local part = workspace.Part
local MaxHealth = 100
function updateColor()
local _hue, _sat, _val = part.Color:ToHSV()
_val = part:GetAttribute('Health')/MaxHealth
part.Color = Color3.fromHSV(_hue, _sat, _val)
end
updateColor()
part:GetAttributeChangedSignal('Health'):Connect(updateColor)
The block on the left is the color I want when the value is 100%.
The block on the right is what it changes to after running the code. - Not the color I want.
You can use math.clamp(number, min, max) to achieve this.
local part = workspace.Part
local MaxHealth = 100
local MAX_HSV_VALUE = 80 -- Example
function updateColor()
local _hue, _sat, _val = part.Color:ToHSV()
_val = math.clamp((part:GetAttribute('Health') / MaxHealth), 0, MAX_HSV_VALUE)
part.Color = Color3.fromHSV(_hue, _sat, _val)
end
updateColor()
part:GetAttributeChangedSignal('Health'):Connect(updateColor)