Help with healthbar

  1. What do you want to achieve?

A health bar system that tweens low when a person takes damage

  1. **What is the issue?

it wont work

  1. What solutions have you tried so far?

youtube tutorials

script:

local tweenserv = game:GetService("TweenService")

game:GetService("GuiService"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local character = game.Players.LocalPlayer.Character

local hum = character:WaitForChild("Humanoid")

local health = hum.Health

local maxhealth = hum.MaxHealth

local greenHB = script.Parent.Frame.HB_Green

local redHB = script.Parent.Frame.HB_Red


local function Update()
	script.Parent.Frame.TextLabel = math.floor(health .. "%")
	greenHB:TweenSize(UDim2.new(health / maxhealth, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15 , true)
end

hum.GetPropertyChangedSignal("Health"):Connect(Update)
``
1 Like

It may be because you have used a period instead of a colon near hum.GetPropertyChangedSignal(). Change it to hum:GetPropertyChangedSignal("Health"):Connect(update)

1 Like

that didn’t fix the problem, still wont move

You have to move health var inside of update func. It is only getting the current health once.

1 Like

yeah?

game:GetService("GuiService"):SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

local character = game.Players.LocalPlayer.Character

local hum = character:WaitForChild("Humanoid")

local greenHB = script.Parent.Frame.HB_Green

local redHB = script.Parent.Frame.HB_Red

local function Update()

local health = hum.Health

local maxhealth = hum.MaxHealth

	script.Parent.Frame.TextLabel = math.floor(health .. "%")

	greenHB:TweenSize(UDim2.new(health / maxhealth, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15 , true)
end

hum:GetPropertyChangedSignal("Health"):Connect(Update)

but still didnt work

let me share a video

Are there any errors being outputted?

no

The error is preventing the rest of the script from running

oh now it says

“Players.Teir199.PlayerGui.HealthbarGui.HB_function:15: invalid argument #1 to ‘floor’ (number expected, got string)”

You should change make chat into

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

And make sure that the humanoid health isn’t nil before trying to floor it. Try running it through a tonumber()

1 Like

ok ill try this tommorow tommorow tommorow

local starterGui = game:GetService("StarterGui") --needs to be StarterGui
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait() --wait for character if it doesn't already exist yet
local humanoid = character:WaitForChild("Humanoid")

local gui = script.Parent
local frame = gui:WaitForChild("Frame")
local textLabel = frame:WaitForChild("TextLabel")
local greenHB = frame:WaitForChild("HB_Green")

local function onHealthChanged(health)
    textLabel.Text = math.floor(health).."%" --you need to change the text label's text property
    --also concatenate after the number has been floored
	greenHB:TweenSize(UDim2.new(health / humanoid.MaxHealth, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.15 , true)
end

humanoid.HealthChanged:Connect(onHealthChanged) --HealthChanged passes the humanoid's new health as an argument to the connected function
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
2 Likes

yeah so it… worked… but now this happens

UDim2.new(health / humanoid.MaxHealth, 0, 1, 0)
You need to change this so that it works with the Gui you’ve created.

1 Like

alright it worked perfectly, thank you and everyone else who help me! :grin::+1:

(UDim2.new(health / humanoid.MaxHealth * 4.64, 0, 0.37, 0)

now all i have to do is fix the health number to show without having to take damage

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.