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

why won't (while true do) not work in a health bar billboardgui?

When I change the health of the part it doesn't move to what it is supposed to be although it's in a while true do

local hum = script.Parent.Parent.Parent.Parent.Humanoid -- Put Humanoid name into MobGUI

while true do
	script.Parent.HealthNum.Text = hum.Health .. "/" .. hum.MaxHealth -- This is Health Humanoid use "/" Health 100/100 or another number
	script.Parent.Healthbar.Size = UDim2.new(hum.Health/50,0,1,0)
	wait()
end

1 comment

0

i edited the script from the free model thats why there are those unnecessary comments

1 answer

✓ Accepted answer

Firstly, use LocalScript. Secondy, use LocalPlayer with Players service. Use product crossed to find the pourcent. Use the event "Changed".

(Humanoid.Health * 100 / Humanoid.MaxHealth) * 0.01

--< Services
local PlayerService = game:GetService('Players')

--< Variables
local Player = PlayerService.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local Humanoid = Character.Humanoid

--< Events
Humanoid.Changed:Connect(function()
	script.Parent.HealthNum.Text = Humanoid.Health .. "/" .. Humanoid.MaxHealth -- This is Health Humanoid use "/" Health 100/100 or another number
	script.Parent.Healthbar.Size = UDim2.new((Humanoid.Health * 100 / Humanoid.MaxHealth) * 0.01, 0, 1, 0)
	wait()
end)

You can use TweenSize for a smooth resize effect.

script.Parent.Healthbar:TweenSize(UDim2.new((Humanoid.Health * 100 / Humanoid.MaxHealth) * 0.01, 0, 1, 0))

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