Help! code issues (health bar)

Hello! I have an error in my code and I don’t know how to fix it, could someone please help me. :frowning: :frowning:

Health localscript:

local player = game.Players.LocalPlayer

while wait() do
	script.Parent.Size = UDim2.new(0, (player.Character:WaitForChild("Humanoid").Health.."/"..(player.Character:WaitForChild("Humanoid").MaxHealth*275),.035,0))
	
	script.Parent.Parent.Indicator.Text = "Health ".. math.floor(player.Character:WaitForChild("Humanoid").Health).."/"..(player.Character:WaitForChild("Humanoid").MaxHealth)
end

Indicator localscript:

script.Parent.Parent(Enum.CoreGuiType.Health, false)

Images of the life bar and how it is organized

p1

p2

Please could someone help me to fix this error
I need to make the life bar go down when the player takes damage

You are setting your Bar size to a string instead of an actual number.
You can fix this by writting:

script.Parent.Size = UDim2.new(0, (player.Character:WaitForChild("Humanoid").Health / (player.Character:WaitForChild("Humanoid").MaxHealth*275),.035,0))

i got another error in my code:
script.Parent.Parent(Enum.CoreGuiType.Health, false)
12

You also could make a Local script inside of “Health” and try

local health = game.ReplicatedStorage.health
local player = game.Players.LocalPlayer

while wait() do
	health.Value = player.Character:WaitForChild("Humanoid").Health
end

Then You move into Replicated Storage, and add in a IntValue.

Instead of doing that you can simply write.

local StarterGui = game:GetService("StarterGui");
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false);

script.Parent.Parent(Enum.CoreGuiType.Health, false)

Is referring to the Game GUI itself, and it needs to be functioned with :SetCoreGuiEnabled; you can easily fix it by just putting in:

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

got this error :frowning:
23
and this is my health size
size

To add on, running it in a loop is bad practice. It can slow down the game. For better performance, I suggest running it in something like this:

Humanoid.HealthChanged:Connect(function()
      -- Code
end)
1 Like

I still have the problem and it tells me that it is because of a “,” how can I solve that?


p1 p2 size

script.Parent.Size = UDim2.new(0, (player.Character:WaitForChild(“Humanoid”).Health / (player.Character:WaitForChild(“Humanoid”).MaxHealth*275),.035,0))

I’m wondering why you’re changing the Offset Properties & not the Size :thinking:, there’s another simple way to do it and here’s an example:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Frame = script.Parent

Humanoid.HealthChanged:Connect(function()
    local Difference = Humanoid.Health / Humanoid.MaxHealth
    Frame.Size = UDim2.new(Difference, 0, .035, 0)
    Frame.Parent.Indicator.Text = "Health "..math.floor(player.Character:WaitForChild("Humanoid").Health).."/"..(player.Character:WaitForChild("Humanoid").MaxHealth)
end)

(I haven’t tested this but I think it will work)

1 Like

You also could make a Local script inside of “Health” and try

local health = game.ReplicatedStorage.health
local player = game.Players.LocalPlayer

while wait() do
	health.Value = player.Character:WaitForChild("Humanoid").Health
end

Then You move into Replicated Storage, and add in a IntValue.

1 Like

Your code is working but my health bar size is changed… how could i fix that :frowning:

robloxapp-20210113-1248516.wmv (2,0 MB)

p2

size

I guess you can try it with the Offset property again
Replace line 8 with this and see if you get any difference?

Frame.Size = UDim2.new(0, (Difference*275),.035,0)

(Also noticed the error in Post #9 was that there was an extra parenthesis ), so I went ahead and briefly fixed that)

1 Like

It worked! thanks you a lot! :smiley:
As a last question, how could I adjust the “.35” does not change in size, it also changes size and becomes much smaller ingame

I don’t think I understood your question right, are you wanting to change the Y Size Property whenever a Player takes damage? (Or do you want it to not change whenever a Player takes damage)

I mean the size Y .035, in ingame this changes and gets smaller

You’ll probably need to configure around with the Difference variable, maybe something like this?

Frame.Size = Udim2.new(0, (Difference*275), (Difference / -350) + .035, 0)

I’m not certain that this will work, but I’m just giving a broad idea on what I think how you can change it

Only Y .035 change and get smaller :frowning:
555

Ooooh I see my bad, I think you can just set the Y Size property equal to the size of the Health Overlay’s Y Size, so:

Frame.Size = Udim2.new(0, (Difference*275), --Whatever the Gray's Frame Size Y is, 0)

(Or you can just guess how far to make the Y size property whatever works really lol, we should also probably talk in a PM so that we’re not spamming this post)

Y size get smaller :frowning:
rr3

The code:
Frame.Size = UDim2.new(0, (Difference*275),.35, 0)

Frame size:
size