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

Extremely weird problem with healthbar?

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.

5 comments

0

What's the GUI object "Health's" size values?

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

0

that is because the background/health is 200 pixels long

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.

1 answer

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!

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