When I respawn the Healthbar doesn't come back

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)
2 Likes

Try enabled ResetGuiOnDeath on the screengui and move the handler healthbar script to the gui itsself

1 Like

The Health Bar script is managed in the player, do you think I should put the script in StarterGui?

image

3 Likes

And also, I’m using a part, which is why I said “3D healthbar”

1 Like

oh i didnt know 3d part nvm then

Ok so I have looked over your post and I believe your problem is caused because you only defined healthBar at the top of the script and once you clone it again once you respawn the function healthBarPosition() doesn’t account for the new health bar. to fix this you could probably add a new healthBar = workspace.healthBarGUI in the player.CharacterAdded function or something like that lol

P.S. I don’t really use DevForum and I just looked here for fun bc I’m at school and idk what to do :smile:

2 Likes

Try moving the clone function to whenever the character loads and have a variable named something such as CurrentHealthbar which you can then use in an if statement to delete if it still exists when the player respawns before loading in a new one.

I’m not sure if this will solve the issue but I hope it helps

Maybe put a wait and then clone the respawn sound.

u can try having the healthbar logic in “PlayerAdded” and loading the character’s humanoid in the function, something like

Players.PlayerAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
    	humanoid.HealthChanged:Connect(function() healthChanged(humanoid) end)
    	healthChanged(humanoid)
end)

function healthChanged(humanoid) -- Add humanoid to arguments
	-- rest of code
end
1 Like

This will make it only work when the player joins the game.

your right, maybe CharacterAdded instead (I think its one of the 2?)

Yeah that’s what I am currently using

Ok, well I tried doing something similar to that but for some reason player.CharacterAdded is not firing at all.

As seen by the print functions, A3 is the only string that gets printed into the output, and so when I get off the title screen I get an error that says the health bar is nil.

local replicatedStorage = game:GetService("ReplicatedStorage")
local healthBar
local healthbarMaxYSize = 0.75

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera

print("A3")
player.CharacterAdded:Connect(function(char)
	print("A1")
	if healthBar then
		healthBar:Destroy()
	end
	print("A2")
	healthBar = replicatedStorage:WaitForChild("Assets"):WaitForChild("Player"):WaitForChild("healthBarGUI"):Clone()
	healthBar.Parent = workspace.Terrain
	healthBar.CanCollide = false
	
	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()
	if game.Players.LocalPlayer:GetAttribute("OnTitleScreen") == false then
		game:GetService("RunService").Heartbeat:Connect(healthBarPosition)
		humanoid.HealthChanged:Connect(healthChanged)
	end
end)

I think this might explain it


Maybe you can try to move the script somewhere else?

Putting it in StarterGUI did nothing.

If you’re using CharcaterAdded then maybe try removing that as when the player respawns, it will create a new script inside of the character and if you use the CharacterAddded function, it won’t fire as the script will have been created with the character so i don’t think it be able to detect a character being added.

If you did this you would have to destroy the health bar when the character is destroyed (as the player respawns). In addition, you could also parent it to StarterPlayerScripts as this may help to solve the issue which i mentioned above.

Hope this helps :slight_smile:

I noticed something in your video. Your old health part still exists so maybe you are viewing the old one instead of the new one. Try erasing the old one when you create a new one.