Anyway to optimize this script?

I’m looking for a way to optimize this script, make it better, something like that…
This is the code:

while true do
	wait(0.1)
	local Health = game.Players.LocalPlayer.Character.Humanoid.Health
	local NewHealth = string.format("%.0f", Health)
	script.Parent.Text =  NewHealth .. "/" .. game.Players.LocalPlayer.Character.Humanoid.MaxHealth
end

I tried this:

local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()
Player.Character.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local Health = Player.Character.Humanoid.Health
	local NewHealth = string.format("%.0f", Health)
	script.Parent.Text =  NewHealth .. "/" .. Player.Character.Humanoid.MaxHealth
end)

and didn’t work, idk why, nothing shows up on output

1 Like

You can use the HealthChanged event, as it does what you’re looking for.
So your code would become like this:

local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()
Player.Character.Humanoid.HealthChanged:Connect(function(Health)
	local NewHealth = string.format("%.0f", Health)
	script.Parent.Text = NewHealth .. "/" .. Player.Character.Humanoid.MaxHealth
end)

The Health parameter is the new value of the Humanoid’s health.

1 Like

Hi,

Your Code looks fine, but I have some concerns over a couple of things I noticed


To Start off, lets talk about your Variables, instead of doing Player.CharacterAdded:Wait() by itself, it would be best to do:

local Character = Player.Character or Player.CharacterAdded:Wait()

or (but I wouldn’t recommend this method as it can be swapped with the method above):

repeat wait() until Player.Character

With the Player.Character or Player.CharacterAdded:Wait() code, if the Character doesn’t exist, it will wait until it (Not with all :Wait() code) returns an Instance, in this case, it returns the Player’s Characater.


Your Code on the first and second example is not recommended, It will constantly run and may cause a form of performance issues depending on how many things are changing at a time.
While both codes are efficient, the while loop is definitely a more inefficient way of doing it, which is why you use Changed, or GetPropertyChangedSignal, However the Humanoid has its own Event in which is specialized for Health Change: Humanoid.HealthChanged

Both HealthChanged and GetPropertyChangedSignal("Health") are practically the exact same thing, but I would recommend HealthChanged as it is specialized for the Humanoid’s Health and for your code to be less wordy, it also returns the Humanoid’s Health In which GetPropertyChangedSignal("Health") does not, It only detects a change that fires code.

Character.Humanoid.HealthChanged:Connect(function(Hp)
    Item.Text = string.format("%.0f", Hp).." / "..Char.Humanoid.MaxHealth
end)

For your string.format, I think you may have made a mistake?

if you want to have a decimal in your value, you would need to do "%.1f", "%.2f" and so on.

"%.1f" would return 100.0
"%.2f" would return 100.00

However if that is not the case and is intended, than I would recommend:

  • math.round()
    This will just round based on what the number is at, so if below .5, it will round down, but if above, it will round up.

  • math.floor()
    Rounds to the lowest value, if you have 1.9, it will automatically round to 1

  • math.ceil()
    Rounds to the highest value, if you have 2.1, it will automatically round to 3

You can do this if you like, its up to you.


Your Code should work overall, it just depends on how to format your code, or how efficient it is compared to other methods.

I Hope this helps.

2 Likes

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