Tween Not Smooth

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)

Nope it’s not working just tested it right now

Instead of making it 0.3 seconds try 1

I also tried it but it’s not working i also tried big numbers like 900000 but it still just feels like there’s no tween

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

Maybe try Enum.EasingDirection.InOut and if that doesn’t work I would look at this document to see what each animation does UI Animations | Documentation - Roblox Creator Hub

lerping works for UIs plus i need a loop to make it smooth no ?

I’m arleady using Enum.EasingDirection.InOut

the issue could possible be that you have a space between ) })

from:

TweenService:Create(healthBar,TweenInfo.new(0.3,Enum.EasingStyle.Linear,Enum.EasingDirection.In),{Size = UDim2.new(healthPercent * xSacle / 100,0,0.033, 0) }):Play()

to:

TweenService:Create(healthBar, TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Size = UDim2.new(healthPercent * xSacle / 100, 0, 0.033, 0)}):Play()

Yes lerping works for uis too and yes you would need to loop it with task.spawn i think but i think you can do it without looping it

How is your Health Bar setup in the UI?

I tried lerping it also doesn’t work here’s the script that i used if i did anything wrong :

for interpolation = 0 , 1,0.001 do
	healthBar.Size:Lerp(UDim2.new(healthPercent * xSacle / 100, 0, 0.033, 0),interpolation)
end

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)

Your script does work but my UI is too big :

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.

Thank you so much i copied the same healthbar for the background without removing its scripts haha :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.