How would I go about making the over health bar to update as a player loses health on the spot without any delays?
The current health bar I have has a a bit of a delay when updating a player when they take damage
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
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:
(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”)
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.
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
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