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

If's and BackgroundColor3? [Answered]

What I'm trying to do is when the player's health reaches a certain point, a gui's background color changes to a certain color.

75+ = Green

50-75 = Orange

25-50 = Red

25- = Really Red
--To round the player's health on a gui
local function Round(x)
    return math.floor(x+0.5)
end

local percent = script.Parent.Indicator.Percent
local indicator = script.Parent.Indicator
local player = game.Players.LocalPlayer
workspace:WaitForChild(player.Name)
local char = game.Workspace[player.Name].Humanoid
--green = 60, 255, 0
--red = 255, 38, 0
--reallyred = 255, 0, 0
--orange = 255, 160, 0 

char.Changed:connect(function()
	indicator.Size = UDim2.new(char.Health/100, 0, 1, 0)
	percent.Text = Round(char.Health).."%"
	if char.Health >= 75 then --Green
		indicator.BackgroundColor3 = Color3.new(60, 255, 0)
	elseif char.Health >= 50 and char.Health <= 75 then --Orange
		indicator.BackgroundColor3 = Color3.new(255,160, 0)
	elseif char.Health <= 50 and char.Health >= 25 then --Red
		indicator.BackgroundColor3 = Color3.new(255, 38, 0)
	elseif char.Health <= 25 then --Really Red
		indicator.BackgroundColor3 = Color3.new(255, 0, 0)
	end
end)

--For testing purposes
local healthh = math.random(1,99)
wait(10)
char.Health = char.Health - healthh

1 answer

Added /255 to the end of the colors as they are a Color3 value

--To round the player's health on a gui
local function Round(x)
    return math.floor(x+0.5)
end

local percent = script.Parent.Indicator.Percent
local indicator = script.Parent.Indicator
local player = game.Players.LocalPlayer
workspace:WaitForChild(player.Name)
local char = game.Workspace[player.Name].Humanoid
--green = 60, 255, 0
--red = 255, 38, 0
--reallyred = 255, 0, 0
--orange = 255, 160, 0 
--yellow = 255, 255, 0

char.Changed:connect(function()
	indicator.Size = UDim2.new(char.Health/100, 0, 1, 0)
	percent.Text = Round(char.Health).."%"
	if char.Health >= 75 then --Green
		indicator.BackgroundColor3 = Color3.new(60/255, 255/255, 0/255)
	elseif char.Health >= 55 and char.Health <= 75 then --Orange
		indicator.BackgroundColor3 = Color3.new(255/255,160/255, 0/255)
	elseif char.Health >= 50 and char.Health <= 55 then
		indicator.BackgroundColor3 = Color3.new(255/255, 255/255, 0/255)
	elseif char.Health <= 50 and char.Health >= 25 then --Red
		indicator.BackgroundColor3 = Color3.new(255/255, 38/255, 0/255)
	elseif char.Health <= 25 then --Really Red
		indicator.BackgroundColor3 = Color3.new(255/255, 0/255, 0/255)
	end
end)

--For testing purposes
local healthh = math.random(1,99)
wait(10)
char.Health = char.Health - healthh

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