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
local function updateLvl()
levelLabel.Text = "Lvl: "..plr.Level.Value
end
updateLvl()
plr.Level.Changed:Connect(updateLvl)
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)
2 comments
Did u try printing to see if the code is running? Is this a server script?
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