Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Health Bar size going out of the screen?

Hello.

I am currently trying to make a Health Bar for a player, that upon taking damage, a health bar goes down depending how much health the player loses. This works, but the issue that I am facing is that the health bar goes off the screen completely, it looks like This. I am unsure the cause of this. Here is the script:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character

wait()
local Humanoid = Character.Humanoid

local Frame = script.Parent
local HealthText = Frame.Parent.Parent.HealthLabel

local RunService = game:GetService("RunService")

local function UpdateHealth(PlayerHealth)
	Frame:TweenSize(UDim2.new(PlayerHealth, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quad, 0.15)
	HealthText.Text = "Health: " .. Humanoid.Health
end

RunService.RenderStepped:Connect(function()
	if Character then       
		UpdateHealth(Humanoid.Health)
	end
end)

I am unsure the cause of this, and would like some help. Thanks!

1 answer

✓ Accepted answer

That's because you are setting X scale of the Health bar to PlayerHealth variable, if player has 100 health, it will be 100 times larger than object that this bar is parented to, you must get max health of the player and in some cases (not in yours if i am sure) default size of the health bar to make it work.

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild('Humanoid')

local Frame = script.Parent
local HealthText = Frame.Parent.Parent.HealthLabel

local function UpdateHealth()
	local PlayerHealth = Humanoid.Health

	Frame:TweenSize(
		UDim2.fromScale(PlayerHealth / Humanoid.MaxHealth, 1),
		Enum.EasingDirection.Out, Enum.EasingStyle.Quad,
		0.15
	)
	
	HealthText.Text = "Health: " .. PlayerHealth
end

Humanoid:GetPropertyChangedSignal('Health'):Connect(UpdateHealth)

I assume your HealthText variable has default size 1, 0, 1, 0, in this case all you do is divide humanoid max health with humanoid current health. I used GetPropertyChangedSignal which returns an event which fires every time given property of the instance changes, in this case it's health, you could use HealthChanged but it does not fire if new health is higher than max health so i am not sure if it would be the best option for a health bar. In your case you used RunService which you should not because it just eats the memory for no reason and fires the function for no reason.

You also used wait at the start i assume to wait for the character to load? Don't do that, it won't work, you also did not wait for the character, i used CharacterAdded event which fires when the character is added and added :Wait which is a function that waits until given event fires and returns what the event returns, that way you can be sure that the character exists, next i used WaitForChild for the humanoid, it will yield until humanoid is found so no need for wait.

Sources

CharacterAdded

GetPropertyChangedSignal

Humanoid.HealthChanged

:Wait

WaitForChild

1 comment

0

That worked spectacularly. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.