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

how do I fix this custom health bar issue?

Asked by 5 years ago

I got this code sample from an article on Roblox developer:

  • https://developer.roblox.com/en-us/articles/Making-a-Unit-Frame

here is the code, I basically copied everything perfectly- other than changing the size and position

01local player = game.Players.LocalPlayer
02local unitFrame = script.Parent
03 
04local healthBar = unitFrame.HealthBarContainer.HealthBar
05 
06player.CharacterAdded:Connect(function(character)
07    local humanoid = character:WaitForChild("Humanoid")
08    humanoid.HealthChanged:Connect(function(health)
09        local healthPercentage = health / character.Humanoid.MaxHealth
10        healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
11    end)
12end)

I'm thinking maybe this is outdated? what should I change?

0
Wouldn't it be local healthPercentage = character.Humanoid.MaxHealth - health Warriorfoox 25 — 5y
0
no that would be how much health is remaining. If i have 50% of my health left I want 50% of the bar to be filled, which is what divide currentHealth by maxHealth does Spiritlotus 151 — 5y
0
Use this for reference point to: https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged I think this will help you. Warriorfoox 25 — 5y
0
what exactly is the problem here? is it that the bar doesnt move, is it a problem in what direction it moves in, etc. theking48989987 2147 — 5y

1 answer

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

Unsure if I'm 100% correct here but I think the character is being added before the .CharacterAdded event is set up. This might be because GUIs and local scripts put in StarterGui only load after the character is loaded. You could try getting the character directly first and if it isn't nil, use that character.

This is what I did to fix it:

01local player = game.Players.LocalPlayer
02local unitFrame = script.Parent
03 
04local healthBar = unitFrame.HealthBarContainer.HealthBar
05 
06local function CheckHealth(character)
07    local humanoid = character:WaitForChild("Humanoid")
08    humanoid.HealthChanged:Connect(function(health)
09        local healthPercentage = health / humanoid.MaxHealth
10        healthBar.Size = UDim2.new(healthPercentage, 0, 1, 0)
11    end)
12end
13 
14if player.Character ~= nil then
15    CheckHealth(player.Character) -- If the character is found, it runs the function and gives the character.
16end
17 
18player.CharacterAdded:Connect(CheckHealth) -- If the character is nil or is respawning, this should run the function and give the new character once the character spawns.

Hope this helped! If you have any questions, just comment!

0
you could also just do:local character = player.Character or player.CharacterAdded:Wait() theking48989987 2147 — 5y
0
thank you so much for this, it worked exactly how I wanted it to Spiritlotus 151 — 5y
0
I now realize I was very vague when asking the question lol but Im glad you knew what was going on Spiritlotus 151 — 5y
Ad

Answer this question