I have this small code for a health bar:
It's in a local script
local player = game.Players.LocalPlayer local health = player:WaitForChild("Humanoid").Health player.Humanoid.Changed:connect(function() script.Parent.Size = UDim2.new(0,health*3.6,2,0) end)
What this does is make the size of the bar the number of health the humanoid.
But if I made the bar only 100 it would be too small so I added the health*3.6
to make it look normal.
In my screen it's perfect but in other peoples screens it only appears half the bar.
I tried using a plugin but it didn't work.
Is there any way to make my script know how full the bar needs to be relatively to the player's health?
Like for it to be full when the health is 100 and half when the health is 50.
Some sort of calculation, or something I am missing..
Sorry if this is a dumb question I just can't seem to figure it out.
Thanks for whoever helps me with this! :)
Before you view my answer, I noticed a few small problems in the script. If you fix these it may fix the script, so try fixing these first:
-The Humanoid isn't a member of Player, it's a member of Player.Character. When getting the Humanoid, use Player.Character:WaitForChild("Humanoid")
instead.
-You can't determine when a number value is changed. A Humanoid's Health is a number value. Instead, determine when the Humanoid itself is changed. Basically instead of using just health
, use Humanoid.Health.
The script at the end of this should fix some of your problems in case you don't get what I'm saying.
The basics of UDim2 in Gui is this (If you already know it then just skip the next two paragraphs):
-The first and third number is the X and Y relative to the screen. For example, if I scripted TextLabel.Size = UDim2.new(1,0,1,0)
, the TextLabel would cover the entire screen. if I instead typed (0.5,0,0.5,0)
, then it'd cover half of the screen.
-The second and fourth number is the X and Y relative to pixels. If I instead used TextLabel.Size = UDim2.new(0,100,0,100)
, it'd make the TextLabel exactly 100 pixels long and 100 pixels wide.
The stuff above is critical to making something like a health bar that changes size. I recommend using the first and third numbers in UDim2 when making something like a health bar. If you put this script into your health bar, it should work:
local player = game.Players.LocalPlayer local humanoid = player.Character:WaitForChild("Humanoid") humanoid.Changed:connect(function() script.Parent.Size = UDim2.new(Humanoid.Health/10, 0, 0, 50)