I’m working on a label that displays the capacity of the player’s bag and wanted to implement a way for the text to get progressively redder as the bag fills up. I have that successfully implemented, with the working code seen below. However, I’d like it to only start turning red once the bag has reached 75% capacity or more.
The following code runs each time the client detects a change in the specific stat for the current capacity.
Capacity is the max, Value is the current value (Value / Capacity)
Assume the Capacity is 100 and the Value is 50. It should not be turning red until Value hits 75 or higher.
local Text = Value .. "/" .. Capacity
local Percentage = math.abs(Value - Capacity) / Capacity
Canvas.PlayerStats.Souls.Label.TextColor3 = Color3.new(1, Percentage, Percentage)
I’ve tried a few different methods but none of them worked properly. Does anyone have any guidance on how to achieve this?
Hey! I did some scripting and I have a solution hopefully for you. The best way I did it was using the percentage, and adding an if statement.
Code with OG Way
local Text = Value .. "/" .. Capacity
--script.Parent:FindFirstChild("TextLabel").Text = Text (I used this as a test, but you may already have this)
local Percentage = math.abs(Value - Capacity) / Capacity
if Percentage <= 0.25 then
Canvas.PlayerStats.Souls.Label.TextColor3 = Color3.new(1, Percentage, Percentage)
else
--Add here what should be the OG color should be, if under 75%
end
Now this works. But the “red” for me wasn’t my liking, as they all looked similar to each other, and if you wanted “different shades of red” may not be way, so I did do a second way, with more control of the red, if you want clear differences.
Code that Allows you to Control the red more
local TableOfReds = {Color3.new(0.756863, 0.478431, 0.490196), Color3.new(0.756863, 0.290196, 0.321569), Color3.new(1, 0.262745, 0.27451), Color3.new(1, 0.113725, 0.129412), Color3.new(1, 0, 0.0156863)}
--Change the Red color of each, to how you would like each red to look like! Lightest to darkest
local Percentage = math.abs(Value - Capacity) / Capacity
if Percentage <= 0.25 then
local numberofRed = 0
repeat
numberofRed += 1
Percentage += 0.05
until Percentage >= 0.25
Canvas.PlayerStats.Souls.Label.TextColor3 = TableOfReds[numberofRed]
else
--Add here what should be the OG color should be, if under 75%
end
Let me know if you have questions or Errors happen, or if this wasn’t quite what you were looking for!