PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Color

BasePart

Color3

The Color property determines the color of a part. If the part has a /BasePart/Material, this also determines the color used when rendering the material texture. If this property is set, /BasePart/BrickColor will use the closest BrickColor to the Color3 value.

Other visual properties of a part are determined by /BasePart/Transparency and /BasePart/Reflectance.


Code Samples


Character Health Body Color

This code sample colors a player’s entire character based on how much health they have. It generates a color based on their max health, then sets the color properties of objects within their character, removing any extra objects.

-- Paste into a Script within StarterCharacterScripts 
-- Then play the game, and fiddle with your character's health
local char = script.Parent
local human = char.Humanoid

local colorHealthy = Color3.new(.4, 1, .2)
local colorUnhealthy = Color3.new(1, .4, .2)

-- Set the color of the humanoid, removing any extra objects
local function setColor(color)
	for _, child in pairs(char:GetChildren()) do
		if child:IsA("BasePart") then
			-- Set the color of any parts
			child.Color = color
			-- Remove decals
			while child:FindFirstChildOfClass("Decal") do
				child:FindFirstChildOfClass("Decal"):Destroy()
			end
		elseif child:IsA("Accessory") then
			-- Set the color of any accessories
			child.Handle.Color = color
			local mesh = child.Handle:FindFirstChildOfClass("SpecialMesh")
			if mesh then mesh.TextureId = "" end
		elseif child:IsA("Shirt") or child:IsA("Pants") then
			-- Remove shirt/pant textures
			child:Destroy()
		end
	end
end

local function update()
	-- Calculate the percentage health the human has
	local perc = human.Health / human.MaxHealth
	
	-- Create a color by tweening based on the percentage of your healthy
	-- The color goes from colorHealthy (100%) ----- > colorUnhealthy (0%)
	local color = Color3.new(
		colorHealthy.r * perc + colorUnhealthy.r * (1 - perc),
		colorHealthy.g * perc + colorUnhealthy.g * (1 - perc),
		colorHealthy.b * perc + colorUnhealthy.b * (1 - perc)
	)
	setColor(color)
end

update()
human.HealthChanged:Connect(update)