I made a healthbar UI, but neither the Health nor the Bar changes when I take damage.
local player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local InterfaceUI = script.Parent
InterfaceUI.Enabled = false
local humanoid = player.Character:WaitForChild("Humanoid")
local function updateHealthBar()
local PlayerHP = humanoid.Health
local PlayerMaxHP = humanoid.MaxHealth
InterfaceUI.BottomLeft.aHealth.OuterBorder.mainFrame.Bar.Size = UDim2.new(PlayerHP / PlayerMaxHP, 0, 1, 0)
InterfaceUI.BottomLeft.aHealth.OuterBorder.mainFrame.TextLabel.Text = PlayerHP.." / "..PlayerMaxHP
end
Camera:GetPropertyChangedSignal("CameraType"):Connect(function()
print("camera changed")
if Camera.CameraType == Enum.CameraType.Scriptable then
print("camera scriptable")
InterfaceUI.Enabled = false
else
print("camera not scriptable")
InterfaceUI.Enabled = true
updateHealthBar()
end
end)
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
print(humanoid.Health)
updateHealthBar()
end)
The TextFrame initially says 0 / 0, and it does update to 100 / 100 when I spawn, but nothing changes when I take damage. print(humanoid.Health) does not run either
It’s possible the Character or Humanoid is not being initialized properly
try
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
end
This is a shot in the dark, but it is considered best practice anyways so it can only help.
Try this, i used your script and it didnt work, after messing around it i fixed it i guess:
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local InterfaceUI = script.Parent
InterfaceUI.Enabled = false
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function updateHealthBar()
local PlayerHP = humanoid.Health
local PlayerMaxHP = humanoid.MaxHealth
if PlayerMaxHP > 0 then
InterfaceUI.BottomLeft.aHealth.OuterBorder.mainFrame.Bar.Size = UDim2.new(PlayerHP / PlayerMaxHP, 0, 1, 0)
InterfaceUI.BottomLeft.aHealth.OuterBorder.mainFrame.TextLabel.Text = math.floor(PlayerHP).." / ".. math.floor(PlayerMaxHP)
end
end
if Camera.CameraType == Enum.CameraType.Custom then
InterfaceUI.Enabled = true
updateHealthBar()
end
Camera:GetPropertyChangedSignal("CameraType"):Connect(function()
if Camera.CameraType == Enum.CameraType.Scriptable then
InterfaceUI.Enabled = false
end
end)
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
updateHealthBar()
end)
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local InterfaceUI = script.Parent
InterfaceUI.Enabled = false
local bar = script.Parent.BottomLeft.aHealth.OuterBorder.mainFrame.Bar
local function onHealthChanged()
local human = player.Character.Humanoid
local percent = human.Health / human.MaxHealth
local defaulthue = 68
-- Change the size of the inner bar
bar.Size = UDim2.new(percent, 0, 1, 0)
bar.Parent.TextLabel.Text = math.round(human.Health).." / "..human.MaxHealth
bar.BackgroundColor3 = Color3.fromHSV(defaulthue*percent, 229, 0)
end
local function onCharacterAdded(character)
local human = character:WaitForChild("Humanoid")
if Camera.CameraType == Enum.CameraType.Scriptable or player.PlayerGui:FindFirstChild("LoadingGui") then
InterfaceUI.Enabled = false
else
InterfaceUI.Enabled = true
end
human.HealthChanged:Connect(onHealthChanged)
onHealthChanged()
end
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
onCharacterAdded(player.Character)
end
this is what i did, and it works great now.
i do need to work on the math of the colour changing though
Just so you know, Color3.fromHSV expects values between 0 and 1 for each of its args.
For hue, divide the intended result by 360 (assuming a scale of 0-360)
For saturation and value, divide the intended result by 255 (assuming a scale of 0-255)