Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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)
0
you do know there is a HealthChanged event of the humanoid right? theking48989987 2147 — 6y
0
and can you print this value? health / maxHealth theking48989987 2147 — 6y
0
Clamp the value using math.clamp. The minimum should be 0, the max should be however much MaxHealth User#19524 175 — 6y
0
I presume incapaz means the health value theking48989987 2147 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

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.

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. MatthewCenance 35 — 6y
0
Nice answer. Too bad I can't upvote this. MatthewCenance 35 — 6y
0
Thanks, I just happen to have experienced issues like this before so yeah. Knineteen19 307 — 6y
Ad

Answer this question