Health Bar help (again ;-;)

Alright so, I have a health bar connected to a level up system, every time you level up you got 20+ max health, health heals over 100+ (many people have had issues with this), when you join if you have 200 health your health automatically sets to 200, what I’m having problem with is when you reset you spawn with 100 health then gradually heal to your max health:

Here is a code I used:

local Player = game.Players.LocalPlayer
local Char = Player.Character

Char:WaitForChild("Humanoid").Died:Connect(function()
	wait(4)
	local Char = Player.Character
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Char.Humanoid.Health = Char.Humanoid.MaxHealth
	spawn(function()
		while true do
			wait(0.4)
			Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
		end
	end)
end)

5 Likes
local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait()

local function UpdateHealth(Humanoid)
	Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Humanoid.Health = Humanoid.MaxHealth
end

game.Workspace.ChildAdded:Connect(function(Child)
	if Child.Name == Player.Name then
		local Character = Child
		local Humanoid = Character:WaitForChild("Humanoid", 30)
		if Humanoid ~= nil then
			UpdateHealth(Humanoid)
		end
	end
end)

Didn’t work :confused:
it’s the same as my previous code

Check your output and see if there are any errors. If so, could you take a screenshot and post it here? Or just tell us what it says?

try this… put it in a server script and put it in serverscriptservice

multi = 20
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		wait(4)
		char.Humanoid.MaxHealth = (player.Stats.HealthLevel.Value* multi) + 80
		wait()
		char.Humanoid.Health = math.huge
		spawn(function()
			while true do
				wait(0.4)
				char.Humanoid.Health = (player.Stats.HealthLevel.Value * multi) + multi
			end
		end)
	end)
end)

The code is somehow damaging me

“Attempt to index nil with ‘CharacterAdded’.” Could you show us the entire script?

how does that make sense? that can only happen if your stats.HealthLevel.Value is less than 1 i think.

This should work:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local Char = plr.Character
		Char.Humanoid.MaxHealth = plr.Stats.HealthLevel.Value * 20 + 80
		Char.Humanoid.Health = Char.Humanoid.MaxHealth
	end)
end)

Sure

local Event = game.ReplicatedStorage:WaitForChild("HealthChange")

game.Players.PlayerAdded:Connect(function(Player)
	wait(4)
	local Char = Player.Character
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Char.Humanoid.Health = Char.Humanoid.MaxHealth
	spawn(function()
		while true do
			wait(0.4)
			Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
		end
	end)
end)

Event.OnServerEvent:Connect(function(Player)
	local Char = Player.Character
	Player.Stats.HealthExp.Value = Player.Stats.HealthExp.Value + 2
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80

	local Char = Player.Character
	Char.Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	local healStep =  Char.Humanoid.MaxHealth * 0.012
	wait(4)
	Char.Humanoid.Health = Char.Humanoid.Health + healStep
end)

local Player = game.Players.LocalPlayer

Player.CharacterAdded:Wait() -- This is where the error popped up

local function UpdateHealth(Humanoid)
	Humanoid.MaxHealth = Player.Stats.HealthLevel.Value * 20 + 80
	Humanoid.Health = Humanoid.MaxHealth
end

game.Workspace.ChildAdded:Connect(function(Child)
	if Child.Name == Player.Name then
		local Character = Child
		local Humanoid = Character:WaitForChild("Humanoid", 30)
		if Humanoid ~= nil then
			UpdateHealth(Humanoid)
		end
	end
end)

i already tested my script it works when i substitue healthlevel.value for 10 or anynumber(that was probably just your health increasing), but you can try this script

When you reset your health goes to 100, but when you spawn in your health stays the same, I need a function that runs when a player dies

playeradded refreshes everytime your character dies, it’ll work

Awesome thanks!!!
It works now!

Yeah, @ttttbs1243 just solved it

You can’t use game.Players.LocalPlayer inside of a server script

Ahh that’s why it didn’t work
Thanks for the help :]

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