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

Extremely weird problem with healthbar?

Asked by
unmiss 337 Moderation Voter
8 years ago

Essentially my healthbar goes 1 pixel off when I set my health back to 100. I just do: health = 50 resizes to 101 health = 100 resizes to 199

What the heck?

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health,false)
local plr = game.Players.LocalPlayer
local rs = game:GetService("RunService").RenderStepped

local quad = Enum.EasingStyle.Quad
local out = Enum.EasingDirection.Out

while plr.Character.Humanoid.Health >= 0 do
    local health = plr.Character.Humanoid.Health
    if plr.Character:FindFirstChild("Health") then
        plr.Character.Health:Destroy()
    end
    script.Parent.Background.Health:TweenSize(UDim2.new(0,health*2,0,5),out,quad,0.4,true)
    wait(0.1)
end

I tried using math.ceil/math.floor to fix the problem but it did absolutely nothing.

0
What's the GUI object "Health's" size values? Redbullusa 1580 — 8y
0
Your problem is line 13; You are looping the Player's Health but also Multiplying the health by 2 (100 * 2 = 200, 50 * 2 = 100, ect.), but, instead of that, why not Divide the Current Health by it's MaxHealth? :P TheeDeathCaster 2368 — 8y
0
that is because the background/health is 200 pixels long unmiss 337 — 8y
0
I'm sorry I can't test it, I don't have ROBLOX Studio installed on the device I am writing this on. Either you accept that I tried or I won't help you anymore. EzraNehemiah_TF2 3552 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

I rewrote your script, I'll tell you everything I did:


Final Product

--LocalScript
function callback() --this is the callback
    return
end

game:GetService("StarterGui"):SetCoreGuiEnabled("Health", false)
local plyr = game:GetService("Players").LocalPlayer
plyr.CharacterAdded:connect(function(char)
    local h = char:WaitForChild("Humanoid") --get player,then character, then humanoid.
    h.HealthChanged:connect(function(health) --instead of loop
        script.Parent.Background.Health:TweenSize(UDim2.new(0,(h.MaxHealth/health)*20,0,5),"Out", "Quad", 1, true, callback()) --replace nil with a function
        --MaxHealth/health
    end)
end)

Hope it helps!

Ad

Answer this question