OverHeadUI Health Bar issue (draining to quickly)

  1. I have this overhead ui health bar set on players. I was wondering how can I set them on dummies as well and also if they reset or if anyone resets make sure that the overheadui remains…
  2. The healthbar is not following the length of the actual healthbar of roblox meaning he is loosing more health on the ui then he actually is…

Video example : Watch 2024-03-20 16-46-07 | Streamable

server side logic

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HealthUpdate = ReplicatedStorage:WaitForChild("HealthUpdate")

local function cloneHealthBarToCharacter(character)
	local healthBarGui = game.ServerStorage:FindFirstChild("HealthBarGui")
	if not healthBarGui then
		warn("HealthBarGui not found in ServerStorage")
		return
	end

	local clonedGui = healthBarGui:Clone()
	clonedGui.Adornee = character:WaitForChild("Head")
	clonedGui.Parent = character

	local humanoid = character:WaitForChild("Humanoid")
	humanoid.HealthChanged:Connect(function(newHealth)
		HealthUpdate:FireAllClients(character, newHealth, humanoid.MaxHealth)
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		cloneHealthBarToCharacter(character)
	end)
end)

local side logic

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HealthUpdate = ReplicatedStorage:WaitForChild("HealthUpdate")

HealthUpdate.OnClientEvent:Connect(function(character, newHealth, maxHealth)
	if not character or not character:IsDescendantOf(game.Workspace) then return end
	local healthBarGui = character:FindFirstChild("HealthBarGui")
	if not healthBarGui then return end
	local healthBar = healthBarGui:FindFirstChild("HealthBar")
	if not healthBar then return end

	local originalSize = healthBar.Size
	local newWidth = originalSize.X.Scale * (newHealth / maxHealth)
	local newSize = UDim2.new(newWidth, 0, originalSize.Y.Scale, 0)

	local TweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(
		0.5,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)

	local tween = TweenService:Create(healthBar, tweenInfo, {Size = newSize})
	tween:Play()
end)

Seems right. Did you try to print newHealth and maxHealth to see if values are right?

I actually did try and it just created a new problem. it’s changing the initial size of the health bar right here:

External Media

this is the local side that is causing the issue

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HealthUpdate = ReplicatedStorage:WaitForChild("HealthUpdate")

HealthUpdate.OnClientEvent:Connect(function(character, newHealth, maxHealth)
	if not character or not character:IsDescendantOf(game.Workspace) then return end
	local healthBarGui = character:FindFirstChild("HealthBarGui")
	if not healthBarGui then return end
	local healthBar = healthBarGui:FindFirstChild("HealthBar")
	if not healthBar then return end

	newHealth = math.min(newHealth, maxHealth)

	local newHealthRatio = newHealth / maxHealth
	local newSize = UDim2.new(newHealthRatio, 0, healthBar.Size.Y.Scale, 0)

	local TweenService = game:GetService("TweenService")
	local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
	local tween = TweenService:Create(healthBar, tweenInfo, {Size = newSize})
	tween:Play()
end)

Maybe try this

Code:

Humanoid:GetPropertyChangedSignal(“Health”):Connect(function()
–decreasing player’s health
end)

I dont understand, that doesnt make sense. please read what I said.

Doesn’t need a RemoteEvent since the properties replicate.

Here’s what I would do (local script):

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local TWEEN_INFO = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)

local function updateOverHeadUI(newHealth, maxHealth, healthBar, originalScale)
	local newHealthRatio = (newHealth / maxHealth) * originalScale
	local newSize = UDim2.fromScale(newHealthRatio, healthBar.Size.Y.Scale)

	local tween = TweenService:Create(healthBar, TWEEN_INFO, { Size = newSize })
	tween:Play()
end

local function onCharacterAdded(character: Model)
	local humanoid = character:WaitForChild("Humanoid") :: Humanoid
	local maxHealth = humanoid.MaxHealth

	local healthBarGui = character:WaitForChild("HealthBarGui")
	local healthBar = healthBarGui:WaitForChild("HealthBar") :: Frame
	local originalXScale = healthBar.Size.X.Scale

	humanoid.HealthChanged:Connect(function(newHealth)
		updateOverHeadUI(newHealth, maxHealth, healthBar, originalXScale)
	end)
end

Players.PlayerAdded:Connect(function(player: Player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)

But the health bar is custom made right. Others need to be able to see your health bar and you need to see your own. If you damage an opponent its applied to the enemy. Considering that it’s server no?

Thanks.

No. If you apply the damage on the server then the health property is replicated to every client. Knowing this, you can just, like the post above said, check whenever the health changes on the client instead of the server for every player. One thing I would add is to also iterate through existing players as well. The PlayerAdded event is only applied to players who join after the client in which the local script is located in.

-- Bottom of the code ZerroxShiot sent
for _, Plr in ipairs(Players:GetPlayers()) do
    local Char = Plr.Character
    if (Char ~= nil) then onCharacterAdded(Char) end
    Plr.CharacterAdded:Connect(onCharacterAdded)
end)
1 Like