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

Help with showing changing health, is it the proper way?

Is this the proper way of writing this script in a local script? The code is changing the text and color of the text based on the health percentage.

local plr = game.Players.LocalPlayer
while true do
	script.Parent.Text = math.floor(plr.Character.Humanoid.Health) .. "%"
	wait(.1)
	if plr.Character.Humanoid.Health <=100 then
		script.Parent.TextColor3 = Color3.new(0/255,255/255,0/255)

		if plr.Character.Humanoid.Health <=75 then
			script.Parent.TextColor3 = Color3.new(235/255,169/255,1/255)
	
	
			if plr.Character.Humanoid.Health <=50 then
				script.Parent.TextColor3 = Color3.new(227/255,122/255,2/255)
	
			
				if plr.Character.Humanoid.Health <=30 then
					script.Parent.TextColor3 = Color3.new(232/255,89/255,22/255)
				
				
					if plr.Character.Humanoid.Health <=10 then
						script.Parent.TextColor3 = Color3.new(255/255,0/255,0/255) 
					end
				end
			end
		end
	end
end


1 answer

Use the HealthChanged event, much easier. I edited the colors a little. Change 'em if you want.

local player=game.Players.LocalPlayer
local humanoid=player.Character.Humanoid


humanoid.HealthChanged:connect(function(health)
	if  health==humanoid.MaxHealth then
		script.Parent.TextColor3=Color3.new(0,1,0) --green 
	elseif  health>=75 then
		script.Parent.TextColor3=Color3.new(1,1,0)
	elseif  health>=50 then
		script.Parent.TextColor3=Color3.new(195/255, 200/255, 0)
	elseif  health>=30 then
		script.Parent.TextColor3=Color3.new(255/255, 65/255, 0)
	elseif health<=10 then
		script.Parent.TextColor3=Color3.new(1,0,0) --red
	end
end)

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