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…
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…
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)
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)
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?
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)