Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My health GUI isn't refreshing, even with a loop, any help?

So I'm working on this health gui above the player's head, and initially it works (says name, level 1, and 100/100 health), but whenever one of those things changes, for example the level, or the health, the gui doesn't change, could I get some help? Thanks

Script:

local par = script.Parent.Parent.Parent.Parent.Parent.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(par)

while wait(1) do
	par.Head.BillboardGui.hp.pln.TextLabel.Text = par.Name
	par.Head.BillboardGui.hp.pll.TextLabel.Text = "Lvl: "..plr.Level.Value
	script.Parent.TextLabel.Text = par.Humanoid.Health.."/"..par.Humanoid.MaxHealth
	script.Parent.Size = UDim2.new((par.Humanoid.MaxHealth/par.Humanoid.Health)/1,-4,1,-4)
	wait()
end

2 comments

0

Did u try printing to see if the code is running? Is this a server script?

0

yeah, if i put print("Ran.") in there, it keeps printing it, but still doesn't work with the actual gui. It's a script inside of the gui which is in starterCharacterscripts under a script that clones it into the head

2 answers

I'll include a helpful page to the roblox wiki at the bottom but here's the script you should use:

--Use a regular script, not a localscript, and place it in either Workspace, StarterScriptService or whatever.

par = script.Parent.Parent.Parent.Parent.Parent.Parent
plr = game:GetService("Players"):GetPlayerFromCharacter(par)

plr.Character.Humanoid.HealthChanged:Connect(function()
    par.Head.BillboardGui.hp.pln.TextLabel.Text = par.Name
    par.Head.BillboardGui.hp.pll.TextLabel.Text = "Lvl: "..plr.Level.Value
    script.Parent.TextLabel.Text = par.Humanoid.Health.."/"..par.Humanoid.MaxHealth
    script.Parent.Size = UDim2.new((par.Humanoid.MaxHealth/par.Humanoid.Health)/1,-4,1,-4)
    wait()
end

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

First, make sure you aren't getting any errors in the Output (equivalently, if you put a print statement inside your while loop, if it keeps printing out about every second, then nothing is erroring). A single spelling mistake can ruin the whole loop.

If there are no errors like that, you should check to see if:

  • there are any other scripts that are competing with yours (ex old versions that are also getting cloned and overwriting your script's changes)
  • maybe your script is working correctly, but the gui you're seeing is different than the one the script is modifying

Second, KingDomas has the right idea - events are generally better to a while loop, since events only run when needed. However, you'll need to put your code into a function that you can then bind to both the HealthChanged event and the player's level changing. Even better, split it up so that you only need to update the aspects of the gui that need to change (see below).

By the way, unlike what Kingdomas has, there's no need to wait() at the end of an event handler. You needn't have multiple waits in a single while loop, either (you have a wait(1) in the condition and a wait() at the end of the loop, which would wait for a combined total of at least 1.03 seconds each time).

Despite this script being longer, it is more descriptive (aside from the assignment, a lot less is going on) and uses events:

local parent = script.Parent
local healthLabel = parent.TextLabel
local character = parent.Parent.Parent.Parent.Parent.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(par)
local hp = character.Head.BillboardGui.hp
local nameLabel = hp.pln.TextLabel
local levelLabel = hp.pll.TextLabel

nameLabel.Text = par.Name -- I'm assuming the name never changes
local function updateLvl() -- define how to update the level
	levelLabel.Text = "Lvl: "..plr.Level.Value
end
updateLvl() -- update the level now
plr.Level.Changed:Connect(updateLvl) -- update the level whenever it changes

local humanoid = character.Humanoid
local function updateHealth()
	healthLabel.Text = humanoid.Health.."/"..humanoid.MaxHealth
    parent.Size = UDim2.new(humanoid.Health/humanoid.MaxHealth,-4,1,-4)
end
updateHealth()
humanoid.HealthChanged:Connect(updateHealth)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(updateHealth)

Note: I made the parent.Size scale based on Health / MaxHealth, so that if you're at 50% health, the gui will be 50% of the maximum width (rather than 2x the width, which is what your code would have done when you effectively had MaxHealth / Health)

3 comments

0

Nope, doesn't work, although I appreciate the effort, and I've tried putting while wait(1) do print("Ran") and that always prints, so I'm really not sure at this point.

0

I should note, if I put the print("Ran") inside one of the events, nothing actually happens for either one.

0

Does the script continue to exist while the character does, or does it destroy itself or get removed by another script?

This discussion is preserved as it was. New answers, comments, and votes are closed.