Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Color3 not fading into the other color properly?

Hi. I was asked to make a stamina bar that fades to black in a project. The problem is, I'm not very good with UIs so I'm pretty much a noob you could say. The color starts at a darkish orange and fades to black as it decreases. Here's the code:

local rgb = {bar.BackgroundColor3.r, bar.BackgroundColor3.g, bar.BackgroundColor3.b}

Bla, bla, bla, so on and so forth...

bar.BackgroundColor3 = Color3.fromRGB(bar.BackgroundColor3.r - (rgb[1] / 200), bar.BackgroundColor3.g - (rgb[2] / 200), bar.BackgroundColor3.b - (rgb[3] / 200))

As soon as the character starts sprinting, it immediately snaps to pitch black. What am I doing wrong? It's probably a silly mistake or a noob question but I really am stumped.

Any help is appreciated. Thanks! ~Loughdough :}

3 comments

0

Please provide all of your code - the problem may be in the loop you use to fade in. I assume a problem could be that you didn't use wait() in your loop?

0

@TheDeadlyPanther using a 0.01 wait loop. i could just use wait() but meh

0

It's basically a while true do loop that checks to see if the player is either idle, walking, or sprinting.

1 answer

✓ Accepted answer

The problem is that you are using Color3.fromRGB, which expects a number from 0 to 255, whereas Color3 is always from 0 to 1. Thus, bar.BackgroundColor3.r is a number from 0 to 1, not 0 to 255. You could multiply it by 255, but easier would be to simply use Color3.new instead of Color3.fromRGB.

(Alternative: Make the bar's BackgroundTransparency go from 0 to 1.)

Ideally you will be able to get an arbitrary distance, so that a player can temporarily stop spending their stamina and then resume spending it before it has fully regenerated. In that case, you might use Color3.lerp, ex:

local originalColor = bar.BackgroundColor3
local targetColor = Color3.fromRGB(0, 0, 0)
--elsewhere, given a 'goal' from 0 to 1:
bar.BackgroundColor3 = originalColor:lerp(targetColor, goal)

1 comment

0

Thanks :)

This discussion is preserved as it was. New answers, comments, and votes are closed.