Over head Health Bar is Slow/Delays

  1. How would I go about making the over health bar to update as a player loses health on the spot without any delays?

  2. The current health bar I have has a a bit of a delay when updating a player when they take damage

  3. I checked for this same issue on other forums however I believe their scripts and where the ui is located is a bit different

local character = script.Parent.Parent.Parent
script.Parent.Parent.HealthBar.Background.Visible = false

if character:FindFirstChild("Humanoid") then
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.HealthChanged:Connect(function(Damage)
		if humanoid.Health <= 99 then
			script.Parent.Parent.HealthBar.Background.Visible = true
			script.Parent.Background.Health:TweenSize(UDim2.new(Damage / humanoid.MaxHealth, 0, 1, 0))
			script.Parent.Parent.HealthBar.Background.Amount.Text = math.floor(humanoid.Health) .."/".. math.floor(humanoid.MaxHealth) .. " HP"
			
		elseif humanoid.Health == 100 then
			script.Parent.Parent.HealthBar.Background.Visible = false
		end
	end)
end
1 Like

I made a healthbar in my own game and this is what I used:

local character = script.Parent.Parent.Parent.Parent
local humanoid = character:WaitForChild('Humanoid')

humanoid.HealthChanged:Connect(function(Damage) -- Checks when the Humanoid's health changes in any way
	script.Parent.Size = UDim2.new(Damage/humanoid.MaxHealth, 0, 1, 0) -- Sets the "CurrentHealth" Frame's position
end)

while wait() do
	script.Parent.Parent.HealthLabel.Text = tostring(math.round(humanoid.Health))..'/'..tostring(humanoid.MaxHealth)..' HP'
end

The script is under these:
image
(The first script parents the gui to the character’s head and the second script I used for changing the size of the frame “CurrentHealth”)

2 Likes

This is what it looks like in-game:
image image

2 Likes
while wait() do
	script.Parent.Parent.HealthLabel.Text = tostring(math.round(humanoid.Health))..'/'..tostring(humanoid.MaxHealth)..' HP'
end

Why the unnecessary while loop? Why not just put it in the changed function?

Ok, I don’t know so much about Frames tween functions, so this could be incorrect, but try changing the 1 value to 0. Why? Because I think that parameter is the time in seconds that has to pass before playing the Tween. Remember that Tweens yield.

Are you running it from a LocalScript or a Server Script? (Ideally you would want to run from a localScript)

Well, if you want to replicate the Health bar to the other users you would use a Server Script or Remote events.

Or you can just do it all on the client since you can access other users health

Something like a Local script that changes your HealthBar and the others HealthBar at the same time? That would be inefficient. Changing the custom HealthBar in a Normal script wouldn’t affect the performance AFAIK.

And remember that Roblox’s HealthBar could work different than GUIs

Changing the health on the server always makes a delay

Change the health on the client then send a remote event to the server to tell the other clients to also make the change on their side

It is technically slower in performance but its completely unnoticeable

How about change Health of people within a certain distance of you

I used .Changed but it didn’t work for some reason, so I used a loop.

You dont need to optimize anything
Its going to take milliseconds to complete
The framerate will be exactly the same

The delay exists because the client and server take some time to communicate over the network
Its not slow because the game is taking too long to compute things

2 Likes

You are right, I suppose. You don’t need to optimize everything or anything.