So I made a 3D health bar that follows the player’s camera, but every time I respawn for some reason the health bar is cloned but it doesn’t move at all and the functions aren’t functioning from what it seems. There are no errors and I’m not sure what to do, but in the video, you can see what I am talking about.
Whenever the character is added, there is a function in the code that is supposed to play a sound but this also doesn’t work after resetting my character…
print("E")
player.CharacterAdded:Connect(function(char)
print("O")
local respawnSound = replicatedStorage:WaitForChild("Assets"):WaitForChild("SoundEffects"):WaitForChild("Spawn"):Clone()
respawnSound.Parent = char.PrimaryPart
respawnSound:Play()
respawnSound.Ended:Connect(function()
print("A")
end)
end)
local replicatedStorage = game:GetService("ReplicatedStorage")
local healthBar = replicatedStorage:WaitForChild("Assets"):WaitForChild("Player"):WaitForChild("healthBarGUI"):Clone()
healthBar.Parent = workspace
healthBar.CanCollide = false
print(healthBar)
local healthbarMaxYSize = 0.75
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
print("E")
player.CharacterAdded:Connect(function(char)
print("O")
local respawnSound = replicatedStorage:WaitForChild("Assets"):WaitForChild("SoundEffects"):WaitForChild("Spawn"):Clone()
respawnSound.Parent = char.PrimaryPart
respawnSound:Play()
respawnSound.Ended:Connect(function()
print("A")
end)
end)
function healthChanged()
local health = humanoid.Health
healthBar.SurfaceGui:FindFirstChild("healthBarGreen"):TweenSize(UDim2.new(0.5, 0, healthbarMaxYSize * (health / humanoid.MaxHealth), 0),
Enum.EasingDirection.In,
Enum.EasingStyle.Quint,
0.5,
true
)
print(UDim2.new(0.5, 0, healthbarMaxYSize * (health / humanoid.MaxHealth), 0))
end
function healthBarPosition()
local tween = game:GetService("TweenService"):Create(healthBar, TweenInfo.new(0.0001, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
CFrame = camera.CFrame * (CFrame.new(0, -7.5, -20) * CFrame.Angles(math.rad(0), math.rad(180), math.rad(-90)))
}):Play()
local tween2 = game:GetService("TweenService"):Create(healthBar:FindFirstChild("healthBarText"), TweenInfo.new(0.0001, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0), {
CFrame = healthBar.CFrame * CFrame.new(-2.5, -.5, 0) * CFrame.Angles(math.rad(0), math.rad(0), math.rad(90))
}):Play()
end
game.Players.LocalPlayer:GetAttributeChangedSignal("OnTitleScreen"):Connect(function()
print("1")
if game.Players.LocalPlayer:GetAttribute("OnTitleScreen") == false then
print("2")
game:GetService("RunService").Heartbeat:Connect(healthBarPosition)
humanoid.HealthChanged:Connect(healthChanged)
end
end)