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

Why isn't this local script resizing the green health bar gui?

local player = script.Parent.Parent.Parent.Parent
local playername = player.Name
local character = game.Workspace:WaitForChild("" .. playername .."")
local humanoid = character:WaitForChild("Humanoid")
local health = script.Parent.Green
local plrhealth = humanoid.Health
local plrmaxhealth = humanoid.MaxHealth
plrmaxhealth = 100
plrhealth = 100
local RunService = game:GetService("RunService")
local e = false
local e2 = 0

RunService.RenderStepped:Connect(function()
    if plrhealth < 100 then
        local damage = 100 - plrhealth
        e = true
        repeat
            health.Size = UDim2.new(health.Size.X.Scale - 0.007, 0, 0.5, 0)
            e2 = e2 + 1
            if e2 == damage then
                e2 = 0
                e = false
            end
        until e == false
    end
end) 

Yes, I am making a health GUI. I got an answer on why it had an error with the end at the very end, but it didn't seem to resize the GUI. (Kudos to Raccoonyz for answering it!) So, here I go again with talking about this. I'm using a brick that runs the humanoid:TakeDamage(5) on touching the brick, and when I touch it I get damaged but the green health bar stays the same. (PS. The health bar is 0.7 in scale for X, I am pretty sure X is the length of GUIS.)

Yet again confused,

Narwhal

1 answer

✓ Accepted answer

Hello again! Here is an example of a working script.

RunService.RenderStepped:Connect(function()
	local health = game.Players.LocalPlayer.Character.Humanoid.Health
        local normalx = 598 -- The normal X offset. This uses offset, by the way.
	script.Parent.Size = UDim2.new(0, normalx * health * 0.01, 0, 100)
end) 

Assuming that the script is in a frame and is a local script, it will adjust to the player's health.

To explain how it works, it multiplies the normal x offset by the player's health times 0.01. So for example, if the player's health was 50, it would set the offset to 1/2 of what it normally would be as 50 * 0.01 = 0.5. If the health is 100, the offset is the same.

4 comments

0

Does it have to be offset? I'd rather it be scale, but I don't know if I change it to scale if it will break?

0

It will break if you change it to scale.

0

Nevermind, it will not.

0

mk thanks!

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