You can't just make the health rotate to the player because as there is more than one player in game it wouldnt know what player to rotate to.
what you could make is create a local script that creates a health bar block for every character in game and add bind its movement to renderstep. As it was created by a local part it wont be seen by others, meaning there wont be multiple bars floating around your head.
local bar = Instance.new("Part")
bar.Anchored = true
bar.CanCollide = false
bar.Size = Vector3.new(3,1,1)
bar.BrickColor = BrickColor.new("Bright red")
local barfill = bar:Clone()
barfill.Name = "Fill"
barfill.Parent = bar
barfill.BrickColor = BrickColor.new("Bright green")
local owner = Instance.new("ObjectValue")
owner.Name = "Owner"
owner.Parent = bar
local bars = {}
local runservice = game:GetService("RunService")
local cam = workspace.CurrentCamera
runservice.RenderStepped:Connect(function(step)
for i=1,#bars do
if bars[i] then
if bars[i]:findFirstChild("Owner") and bars[i].Owner.Value ~= nil then
bars[i].Parent = workspace
local offset = Vector3.new(0,-5,0)
bars[i].CFrame = CFrame.new(bars[i].Owner.Value.HumanoidRootPart.Position+Vector3.new(0,5,0),cam.CFrame.Position+offset)
local hppercentage = bars[i].Owner.Value.Humanoid.Health/bars[i].Owner.Value.Humanoid.MaxHealth
bars[i].Fill.Size = Vector3.new(hppercentage*bars[i].Size.X,bars[i].Size.Y*1.1,bars[i].Size.Z*1.1)
bars[i].Fill.CFrame = bars[i].CFrame*CFrame.new((bars[i].Size.X-bars[i].Fill.Size.X)/2,0,0)
else bars[i]:Destroy()
end
end
end
end)
function createHealthBar(char)
local hp = bar:Clone()
hp.Owner.Value = char
table.insert(bars,1,hp)
end
function playerEntered(player)
if player.Character then
createHealthBar(player.Character)
player.CharacterAdded:Connect(createHealthBar)
end
end
game.Players.PlayerAdded:Connect(playerEntered)
local s = game.Players:GetChildren()
for i=1,#s do
playerEntered(s[i])
end
3 comments
I don't know why I can't use the Code block thing in this, It wont surround my actual code. Instead it surrounds either everything or just my text and not my code.
Just paste in your code, highlight it, and then hit the codeblock button.
Fixed the text block problem, apparently you cant tab the beginning of a paragraph