Health Bar not working after death (CharacterAutoLoads disabled)

Hello!

  1. What do you want to achieve? I want to fix my health bar, as I don’t want any bugs in my game.

  2. What is the issue? It breaks after the first death.

  3. What solutions have you tried so far? I’ve tried having it set the character every time it runs the while loop.

Here’s my script: ( local script )

while wait() do
	local plr = game.Players.LocalPlayer
	local char = plr.Character or plr.CharacterAdded:Wait()
	if char then
		local hum = char:WaitForChild("Humanoid")
		print(hum.Parent)
		script.Parent.Size = UDim2.new(0,((hum.Health / hum.MaxHealth) * 100) * 5,0.8,0)
	end
end

Explorer structure:
image

If any more info is needed, I will provide it.

Thanks!

1-you can get player by using parent till the player object from playergui.
2-you just can use the getpropertychangedsignal to size the bar.

try these

Example

local plr = script.Parent.Parent.Parent.Parent.Parent.Parent
plr.Character.Humanoid:GetPropertyChangedSignal(“Health”):Connect(function()
end

Just tried it, did not work. Keep in mind, it might not work because game.Players.CharacterAutoLoads is false.

try this.

task.wait(1)
local plr = script.Parent.Parent.Parent.Parent.Parent
local char = plr.Character or plr.CharacterAdded:Wait()

char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local hum = char:WaitForChild("Humanoid")
	print(hum.Parent)
	script.Parent.Size = UDim2.new(0,((hum.Health / hum.MaxHealth) * 100) * 5,0.8,0)
end)

and also if your gui’s resetonspawn is enabled then it might work better than current one.

Still does not work. I don’t know, CharacterAutoLoads makes it so I need to load the player manually. I don’t know.

oh,to load character manually,i will send you a code that will works if its.

Client:

task.wait(1)
local plr = script.Parent.Parent.Parent.Parent.Parent
local char = plr.Character or plr.CharacterAdded:Wait()

char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
        if hum.Health <= 0 then
               game:GetService("ReplicatedStorage").Respawn:FireServer()
        end
	local hum = char:WaitForChild("Humanoid")
	print(hum.Parent)
	script.Parent.Size = UDim2.new(0,((hum.Health / hum.MaxHealth) * 100) * 5,0.8,0)
end)

Server:

game:GetService("ReplicatedStorage").Respawn.OnServerEvent:Connect(function(Plr)
    task.wait(3) --> your respawn time
    Plr:LoadChararacter()
end)

You just need to add the server script to ServerScriptService.and then create a remote event named: “Respawn”.

No, I already have the loading manually finished. I want to fix the health bar because it breaks afterward.

1-make the screengui resetonspawn checked.
2-then try mine.

task.wait(1)
local plr = script.Parent.Parent.Parent.Parent.Parent
local char = plr.Character or plr.CharacterAdded:Wait()

char.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local hum = char:WaitForChild("Humanoid")
	print(hum.Parent)
	script.Parent.Size = UDim2.new(0,((hum.Health / hum.MaxHealth) * 100) * 5,0.8,0)
end)
2 Likes

okay now it worked, just had to make minor changes to some code and it worked

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.