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

Help with non player Health Bar ?

Asked by 4 years ago

Maybe I'm missing something obvious here? For some reason, "Humanoid.Health" isn't updating, and "Humanoid.HealthChanged" only fires when the Humanoid dies. The script is located withing the GUI I'm using (for healthbar) which is inside the Dummies head. Same script works fine for my player HP bar.

local Hum = script.Parent.Parent.Parent.Humanoid
local NameVal = script.Parent.NameLab
local HPVal = script.Parent.HealthBar.Back.HPLab
local Bar = script.Parent.HealthBar.Bar
HPVal.Text = Hum.Health .. "/" .. Hum.MaxHealth 
Hum.Changed:Connect(function()
    print(Hum.Health)
    wait(2)
end)
Hum.HealthChanged:Connect(function(health)
    HPVal.Text = Hum.Health .. "/" .. Hum.MaxHealth 
    Bar.Size = UDim2.new(Hum.Health/Hum.MaxHealth,0,2,0)
end)

The Hum.Health output never changes from 100, until the Humanoid dies.

0
if the health of the humanoid is being changed through a LocalScript then that could be the problem User#23252 26 — 4y
0
try using a normal script if your using a LocalScript User#23252 26 — 4y
0
No!! This script doesn't can be a ServerScript! It's just possible to make a health bar with LocalScript (or ClientScript). NiniBlackJackQc 1562 — 4y
0
Honestly, a loop can update it. like so: while wait(1) do print(Hum.Health) end DEVLogos 8 — 4y
0
Arsubia12 fixed my problem. It was because the thing I was testing with used a local script to damage. KuroInteriKanashii 4 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

If you want make a health bar you must writte your script on a LocalScript (ClientScript). Place your script on a client storage, ex: StarterGui (or descendance).

local PlayerService = game:GetService('Players') -- Get the players service.

local Player = PlayerService.LocalPlayer -- This is just possible on a LocalScript.
local Character = Player.Character or Player.CharacterAdded:wait() -- In case the character model has already been created

local PlayerGui = Player:WaitForChild('PlayerGui') -- You can't found PlayerGui with the server, so you can't make this script on a ServerScript (Script)

local NameVal = PlayerGui[ name of you screengui ]
local HPVal = ...
local Bar = ... (it's just a links)

Character:WaitForChild('Humanoid'):GetPropertyChangedSignal('Health'):Connect(function()
    HPVal.Text = Hum.Health .. "/" .. Hum.MaxHealth
    Bar.Size = UDim2.new(Character.Humanoid.Health / Character.Humanoid.MaxHealth, 0, 2 ,0)
end)

EXEMPLE

local PlayerService = game:GetService('Players')

local Player = PlayerService.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()

local PlayerGui = Player:WaitForChild('PlayerGui')

local Bar = PlayerGui.ScreenGui.Frame.Frame (Link towards my bar ui)

Character:WaitForChild('Humanoid'):GetPropertyChangedSignal('Health'):Connect(function()
    Bar.Size = UDim2.new(Character.Humanoid.Health / Character.Humanoid.MaxHealth, 0, 1 ,0)
end)

Solved your question? Put in title [SOLVED] or accept a answer.

Ad

Answer this question