So im making a health bar system, and i already got the health bar inner to work, but instead of changing the number i wanted it to be a little cooler and slowly change numbers
Can anyone help me do it?
humanoid:GetPropertyChangedSignal("Health"):Connect(function(newh)
-- this is the part that changes the bar
local t1 = tService:Create(hInner, TweenInfo.new(0.25, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut), {Size = UDim2.new(api.gethum().Health/maxhealth, 0, 1, 0)})
t1:Play()
-- here i wanna make it change the number
end)
I’ve already tweened the bar in the gui, what i wanna tween or whatever is a number (textlabel) that shows the current hp of the player, so it doesnt just instantly changes but instead it slowly changes
Yea I understand but that means you have to stop the tween and make a new one every time there’s a change. You could use the Fusion library and use a spring, in that case it’s super easy
local smoothness = 30
local speed = 2
local endhealth = newh
local starthealth = -- the previous health
local func = math.sin -- function for the "tween"
local startpos = 0
local endpos = math.pi / 2 -- these numbers represent the starting position and end position of the makeshift "tween"
local i = 0
while wait(1 / smoothness) and i < smoothness do
textlabel.Text = math.round(tostring( starthealth + ((func((i / smoothness) * endpos) - func(startpos)) / endpos * (endhealth - starthealth)) ))
i += 1 * speed
end
that should be alright since it will be in a .Changed function? also dont know what is fusion library neither a spring lol, i will wait for @Den_vers 's answer
You cannot tween actual numbers, only properties. So I attempted to make code for tweening. So you can set the smoothness, and the function it uses, but it’s probably broken. I’ve made a health system like this before, and I tweened the GUI but didn’t add an animation for the number. Plus, it may confuse people because they may think they have THIS health, but in reality they have this health because it hasn’t finished tweening yet.
Yes, it only works with actual properties. You’re right you can do transparency and all that stuff. The reason why it doesn’t work with numbers is that you have to actually provide a dictionary of properties to change.
You seemed to have solved your own problem, and this would definitely be the easiest way; but not the most optimal.
I would recommend taking a look at this website; Easing Functions Cheat Sheet if you plan to actually code the tween equation itself. Which shouldn’t be too difficult.
Here is an example usage in which I scripted a while back; you would need to modify it.
self.Maids[TextLabel]:GiveTask(RunService.RenderStepped:Connect(function(DeltaTime)
CurrentEasingTime += DeltaTime
local NormalizedTimeToTake = (CurrentEasingTime / TimeToTakeEasing)
if NormalizedTimeToTake >= 1 then return self.Maids[TextLabel]:Destroy() end
local EasedNumberValue = 1 - math.pow(1 - NormalizedTimeToTake, 4)
TextLabel.Text = self:FormatIntegerWithCommas(ConvertedStringNumber + (Difference * EasedNumberValue))
end))
If you’re looking to avoid scripting the actual easing equation within your code, then you could just make a number value (as previously mentioned), like you seem to want to do.
local NumberValue --// Put this outside of your connection!
--// Put this inside of your PropertyChangedSignal!
if not NumberValue then
NumberValue = Instance.new('NumberValue')
NumberValue.Value = humanoid.MaxHealth
--// Track when the number value changes.
NumberValue:GetPropertyChangedSignal("Value"):Connect(function(Value)
--// Update your GUI Element
TextLabel.Text = Value;
end
end
--// TweenService, you named your variable tService
tService:Create(NumberValue, TweenInfo.new(0.25), {Value = newh}):Play()