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

How do I get the HealthGui to work? [SOLVED]

I have no clue what to change inside the "-- Math" area because it only goes to the left and whenever I regenerate It gets lower. (Inside LocalScript)

-- Variables
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local frame = script.Parent
local hp = frame.HP
local xp = frame.XP
local nextLvl = frame.NextLvl
local hpDisplay = frame.HPDisplay
local stamina = frame.Stamina
local xpDisplay = frame.XPDisplay

-- PropertyChange
local curHp = char.Humanoid.Health
local curMaxHp = char.Humanoid.MaxHealth
hp.Text = 'Hp: '..tostring(curHp)..'/'..tostring(curMaxHp)

-- Events
char.Humanoid.HealthChanged:connect(function()
	local curHealth = char.Humanoid.Health
	local curMaxHealth = char.Humanoid.MaxHealth
	-- Sizes
	local xScale = hpDisplay.Size.X.Scale
	local xOffset = hpDisplay.Size.X.Offset
	local yScale = hpDisplay.Size.Y.Scale
	local yOffset = hpDisplay.Size.Y.Offset
	-- Math
	local percentage = curHealth/curMaxHealth
	local newXOffset = percentage*xOffset
	
	-- Apply
	hpDisplay:TweenSize(UDim2.new(xScale,newXOffset,yScale,yOffset),'Out','Quad',.5,false,nil)
	hp.Text = 'Hp: '..tostring(math.floor(curHealth + .5))..'/'..tostring(curMaxHealth)
end)

1 answer

I figured out that I just had to check the sizes once not every time the health changes

I just put --sizes outside of HealthChanged event

-- Variables
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local frame = script.Parent
local hp = frame.HP
local xp = frame.XP
local nextLvl = frame.NextLvl
local hpDisplay = frame.HPDisplay
local stamina = frame.Stamina
local xpDisplay = frame.XPDisplay
local hum = char:WaitForChild('Humanoid')

-- PropertyChange
local curHp = hum.Health
local curMaxHp = hum.MaxHealth
hp.Text = 'Hp: '..tostring(curHp)..'/'..tostring(curMaxHp)

-- Sizes
local xScale = hpDisplay.Size.X.Scale
local xOffset = hpDisplay.Size.X.Offset
local yScale = hpDisplay.Size.Y.Scale
local yOffset = hpDisplay.Size.Y.Offset

-- Events
char.Humanoid.HealthChanged:connect(function()
	local curHealth = hum.Health
	local curMaxHealth = hum.MaxHealth
	-- Math
	local percentage = curHealth/curMaxHealth
	local newXOffset = percentage*xOffset
	
	-- Apply
	hpDisplay:TweenSize(UDim2.new(xScale,newXOffset,yScale,yOffset),'Out','Quad',.5,false,nil)
	hp.Text = 'Hp: '..tostring(math.floor(curHealth + .5))..'/'..tostring(curMaxHealth)
end)

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