Hello everyone, so i have this health bar in my game and everytime the player loses health a tween will play but it’s not smooth
Here’s what i mean :
Here’s the script :
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local healthBar = player.PlayerGui.BasicUI.HealthBar
local xSacle = healthBar.Size.X.Scale
local yScale = healthBar.Size.Y.Scale
local healthText = player.PlayerGui.BasicUI.HealthNumber
local TweenService = game:GetService("TweenService")
humanoid.HealthChanged:Connect(function(newHeath)
if newHeath > 0 then
local healthPercent = 100 * newHeath / humanoid.MaxHealth
healthBar.Visible = true
TweenService:Create(healthBar,TweenInfo.new(0.3,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Size = UDim2.new(healthPercent * xSacle / 100,0,0.033, 0) }):Play()
--healthBar:TweenSize(,Enum.EasingDirection.In,Enum.EasingStyle.Linear,1,true)
healthText.Text = math.floor(newHeath).."/"..humanoid.MaxHealth
else
healthBar.Visible = false
healthText.Text = "0/100"
end
end)
I wouldn’t really recommend tween for health systems because when you get damage multiple times the tween overrides another I would recommend using lerping
Uhh I don’t really understand what you did there but here’s how I would do it. This is from the top of my head since I’m not home right now so there may be some bugs and error in this
local function HealthChanged()
local Health = math.floor(Humanoid.Health)
local MaxHealth = Humanoid.MaxHealth
local Value = math.clamp(Health/MaxHealth,0, MaxHealth)
local goal = UDim2.fromScale(Value, 1)
HealthBar.Size = HealthBar.Size:Lerp(goal,0.1)
end
local LoopT = task.spawn(function()
while true do
HealthChanged()
task.wait()
end
end)
now when i change it to the normal size using this local goal = UDim2.fromScale(healthPercent * xSacle / 100,0,0.033, 0), 0.033) the smooth effect dissapears
I think i know how to fix it so make a frame and call it Health Bar frame this will be the maximum size the Health-bar can go then parent the health bar to the health-bar frame and also to make the healthbar go back to regular size you have to use switch back to tweenservice
My best guess would be that you have another piece of code affecting the tween. Press ctrl+shift+f and search for .Size and see if something is overriding it. Since you are in team create, I would also try closing the script and reopening the game just to make sure.
Side note: Your easing style is linear so you can just leave easing direction blank like: TweenInfo.new(0.3, Enum.EasingStyle.Linear). Also since it is 0.3 seconds you technically don’t even need an easing style because they would all just look the same, so just doing TweenInfo.new(0.3) would also work.