Healthbar not working

I am trying to make a healthbar, but whenever my heath changes, the healthbar doesn’t shrink, I’ve tried something else but that didn’t work. I’ve run out of ideas and I need help. This is my script so far:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.HealthChanged:Connect(function()
			script.Parent.Size = UDim2.new(0.65, 0, 0.713*game.Players.LocalPlayer.Character.Humanoid.Health/100, 0)
		end)
	end)
end)

(This is a LocalScript)
Thanks for reading this.

local size = math.clamp(humanoid.Health/100, 0, 1)

What would that do? Sorry, I’m new to scripting.

math.clamp() is useful for keeping a number in between a minimum and a maximum, an alternative to try besides 0.713*game.Players.LocalPlayer.Character.Humanoid.Health/100

I don’t see how that would help, the size won’t change at all for me.

Ill try it out though (chars minimum)

I think the problem is you’re using PlayerAdded inside a LocalScript. Use the local player instead.

well that didnt work, what now?

I’ll try that out, thanks (char minimum)

still didnt work for me, i still dont know why

-- Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

-- Player
local Player = Players.LocalPlayer

-- Character / Humanoid
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

-- Connection
Humanoid.HealthChanged:Connect(function()
   local Health = math.clamp((0.713 * Humanoid.Health)/100, 0, 1)
   TweenService:Create(script.Parent, Tweeninfo.new(0.5, Enum.EasingStyle.Linear), {Size = UDim2.fromScale(0.65, Health)}):Play() 
   -- If you don't want a tween you can remove the 2 lines above and replace it with the bottom line
   --script.Parent.Size = UDim2.fromScale(0.65, math.clamp(0.713*Humanoid.Health/100, 0, 1))
end)
1 Like

[quote=“TheDCraft, post:11, topic:1858675, full:true”]

-- Service
local Players = game:GetService("Players")

-- Player
local Player = Players.LocalPlayer

-- Character / Humanoid
local Character = Player.Character or Player.CharactedAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")

-- Connection
Humanoid.HealthChanged:Connect(function()
   script.Parent.Size = UDim2.fromScale(0.65, math.clamp(0.713*Humanoid.Health/100, 0, 1))
end)

Could you explain this script to me? I’d like to use stuff like this in the future.

This still didn’t work for me.

Are you not very familiar with how local scripts work? Your script above is basically set up to do the same thing except TheDCrafts just uses the local player which makes it work.

It’s basically your script but a bit cleaned up and it’s not checking for a player added since it’s a local script and a player should exist when the local script is running.

Where is the script located? Also is there any errors in the output?

It’s located under the healthbar, and I didn’t check for any errors yet, which I’ll do right now.

This is what I got: “Players.Berrinlin.PlayerGui.ScreenGui.In-GameScreen.Health.HealthBar.LocalScript:12: attempt to index nil with ‘HealthChanged’”

CharacterAdded was misspelled in TheDCrafts script.

Nice catch, I didn’t even notice that. I’ve edited the script now.