Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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
0
i edited the script from the free model thats why there are those unnecessary comments Bloxulen 88 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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))
Ad

Answer this question