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

How would I make so the server constantly checks if my health is low?

hi,

how would I make it so the server always checks whether the current_health value is below a certain percentage? Whenever I regenerate my health, the health bar does not update therefore it's still yellow instead of green! Plus sometimes <= or >= don't work.

local health = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local bar = script.Parent.Parent
local current_health = health.Health / 100
local runService = game:GetService("RunService")

health.HealthChanged:connect(function()
runService.Stepped:connect(function()
bar.Size = UDim2.new((health.Health / 100) * 1,0,1,0) 	

 if current_health >=.51 then
	--green
    bar.BackgroundColor3 = Color3.fromRGB(79, 222, 1)
	bar.BorderColor3 = Color3.fromRGB(61, 172, 2)
	print'green'
 elseif current_health <=.5 then
	-- yellow
	bar.BackgroundColor3 = Color3.fromRGB(222, 171, 42)
	bar.BorderColor3 = Color3.fromRGB(173, 133, 32)
	print'yellow'
 elseif current_health <=.3 then
	--red
	bar.BackgroundColor3 = Color3.fromRGB(222, 75, 55)
	bar.BorderColor3 = Color3.fromRGB(166, 55, 41)
	print'red'
--	
       
end
end)
end)



thanks, hydrogyn

1 comment

0

is there a reason you divide the current_health by 100?

2 answers

✓ Accepted answer
local health = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local bar = script.Parent.Parent
local runService = game:GetService("RunService")

health.HealthChanged:connect(function(new_health)

	local current_health = new_health / 100
	bar.Size = UDim2.new((new_health / 100) * 1,0,1,0)   

	 if current_health >=.51 then
	    --green
	    bar.BackgroundColor3 = Color3.fromRGB(79, 222, 1)
	    bar.BorderColor3 = Color3.fromRGB(61, 172, 2)
	    print('green')
	 elseif current_health <=.5 and current_health >=.3 then
	    -- yellow
	    bar.BackgroundColor3 = Color3.fromRGB(222, 171, 42)
	    bar.BorderColor3 = Color3.fromRGB(173, 133, 32)
	    print('yellow')
	 elseif current_health <=.3 then
	    --red
	    bar.BackgroundColor3 = Color3.fromRGB(222, 75, 55)
	    bar.BorderColor3 = Color3.fromRGB(166, 55, 41)
	    print('red'9)
	end

end)

1 comment

0

thanks man! :)

Do a while true do loop with a wait(0.1) inside the script

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