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

This healthbar script is setting the color to a weird value?

So I made a healthbar script that works fine with tweening the position of the bar, but it does not set the color properly, I think it is setting the color to the math it is doing? Code:

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)
local player = game.Players.LocalPlayer
local character = game.Workspace:WaitForChild(player.Name)
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local healthbar = script.Parent.remaining
function checkcolor(health,maxhealth)
	print("calculating color")
	if(health > (maxhealth/3)*2.5) then
		print("setting color to green")
		healthbar.BackgroundColor3 = Color3.new(0, 255, 119)
		end
	if (health <= (maxhealth/3)*2.5) then
		print("setting color to yellow")
		healthbar.BackgroundColor3 = Color3.new(255, 247, 14)
		end
	if (health <= (maxhealth/3)*2) then
		print("setting color to orange")
		healthbar.BackgroundColor3 = Color3.new(255, 144, 8)
		end
	if (health <= (maxhealth/3))then
		print("setting color to red")
		healthbar.BackgroundColor3 = Color3.new(255, 0, 0)
	end
end
function HealthChanged()
	local maxhealth = humanoid.MaxHealth -- for now
	local health = humanoid.Health
	script.Parent.Parent.healthoutof.Text = health.."/"..maxhealth
	script.Parent.Parent.healthoutof.healthoutof.Text = health.."/"..maxhealth
	checkcolor(health,maxhealth)
	healthbar:TweenSize(UDim2.new(health/maxhealth,0,1,0),"In","Linear",0.1)

end
HealthChanged()
humanoid.HealthChanged:connect(HealthChanged)

So if I do something like setting the health to 80, it should set the color to orange, but instead it sets the color to Color3.new(65025, 62985, 3570)

1 answer

✓ Accepted answer

The reason it does this is because of the Color3 values. I don't know why, but the Color3 values have to be from 0 to 1, with 0 being 0, and 1 being 255. So, the simple answer is to divide each value in the Color3 value by 255.

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)
local player = game.Players.LocalPlayer
local character = game.Workspace:WaitForChild(player.Name)
local humanoid = game.Players.LocalPlayer.Character.Humanoid
local healthbar = script.Parent.remaining
function checkcolor(health,maxhealth)
    print("calculating color")
    if(health > (maxhealth/3)*2.5) then
        print("setting color to green")
        healthbar.BackgroundColor3 = Color3.new(0, 1, .46)
        end
    if (health <= (maxhealth/3)*2.5) then
        print("setting color to yellow")
        healthbar.BackgroundColor3 = Color3.new(1, .97, .05)
        end
    if (health <= (maxhealth/3)*2) then
        print("setting color to orange")
        healthbar.BackgroundColor3 = Color3.new(1, .56, .03)
        end
    if (health <= (maxhealth/3))then
        print("setting color to red")
        healthbar.BackgroundColor3 = Color3.new(1, 0, 0)
    end
end
function HealthChanged()
    local maxhealth = humanoid.MaxHealth -- for now
    local health = humanoid.Health
    script.Parent.Parent.healthoutof.Text = health.."/"..maxhealth
    script.Parent.Parent.healthoutof.healthoutof.Text = health.."/"..maxhealth
    checkcolor(health,maxhealth)
    healthbar:TweenSize(UDim2.new(health/maxhealth,0,1,0),"In","Linear",0.1)

end
HealthChanged()
humanoid.HealthChanged:connect(HealthChanged)

Let me know if anything else still goes wrong.

1 comment

0

Alternatively you can use the new Color3 function Color3.fromRGB where you can have whole numbers.

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