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

My health bar script goes out of the box and to the edge of the screen?

this is my script, please help?

game:WaitForChild("Players")
wait(1)
game.Players.LocalPlayer.Character.Humanoid.Changed:connect(function()
	local health = game.Players.LocalPlayer.Character.Humanoid.Health
	local maxHealth = game.Players.LocalPlayer.Character.Humanoid.MaxHealth
	local size = script.Parent.Size
	wait(0.1)
	script.Parent.Text = health
	script.Parent.Parent.Size = UDim2.new(health / maxHealth,0,0,24)
end)

4 comments

0

you do know there is a HealthChanged event of the humanoid right?

0

and can you print this value? health / maxHealth

0

Clamp the value using math.clamp. The minimum should be 0, the max should be however much MaxHealth

0

I presume incapaz means the health value

1 answer

Mk, so here's what I think, I think it's not a problem in the script, it's a problem in the GUI. Assuming the health bar displays your health properly and updates itself, I think you didn't put the health bar frame inside another frame. When you say

script.Parent.Parent.Size = UDim2.new(health/maxHealh,0,0,24)

then it does a health bar across the whole screen because it's scaling the frame across it's parent. To sum it up, to fix it, you need to make another frame in the same screen GUI and put the frame you want to scale inside of it. This new frame needs to be about the size you want the health bar to be and move the scaling frame onto it. If it still scales off a little bit then just do something like

script.Parent.Parent.Size = UDim2.new((health/maxHealth)*0.95,0,0,24)

to make it a little bit shorter. Change 0.95 and test it over and over again until it's the right size.

3 comments

0

Correct. The first and third properties scale the GUI element to the size of the frame it is put in, or if it isn't put in a frame, the screen. Here's a hint. You can color the background frame a different color with the BackgroundColor3 property if you like the generic frame border style, or increase its border size using the BorderSizePixel property.

0

Nice answer. Too bad I can't upvote this.

0

Thanks, I just happen to have experienced issues like this before so yeah.

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