How to do 2 tween's at once?

oh, just keep the tween stored as a variable and either remove it or just use pause() and then garbage collect it (unless :stop() exists, im too lazy to check) @gamie041

Please don’t use a loop for updating your health GUI :pensive: …

If you need, just use the TweenService and create a tween there, it will allow you to cancel any on-going tweens so it doesn’t effect what you’re trying to achieve.

local tween
-- // Just to detect healing, not actually needed
local previous_health = humanoid.Health 
-- seconds it takes for the tween to go from 100% to 0%
local from_100_to_0_duration = 1

local function OnUpdate()
    if (tween) then tween:Cancel() end

    -- // So you can avoid tweening healed health
    -- // This isn't exactly necessary
    local prev = previous_health
    previous_health = humanoid.Health -- // Because I'm too lazy and making a bad example
    if (prev < previous_health) then return end
    
    local percent = humanoid.Health / humanoid.MaxHealth
    -- // This gives a delta, which will allow us to get the correct duration
    -- // for the tween 0:
    local magic = math.abs(script.Parent.Size.X.Scale - percent) -- // So roblox doesn't pull some voodoo stuff
    tween = TweenService:Create(script.Parent, TweenInfo.new(magic * from_100_to_0_duration), {Size = UDim2.fromScale(percent, 1)})
    tween:Play()
end


i dont really think using absolute value is needed here. there will never be a time where the health is greater than maxhealth to cause this problem. its mathematically impossible for it to go into the negatives because of that

edit: my silly little 2 am brain didnt realize what you were using math.abs for nevermind

Let’s say your Scale of the GUI is 50%, and you suddenly heal the avatar the take damage to 60%, you’d have -10%. Plus if you’re healing, you’d be Scale - (Scale + healthGained) which is also negative.

Ah, replied too quick.

yeah i get it my 2 am brain was silly and posted tha reply before realizing

3 Likes

So I shouldn’t use supergeorge2007’s suggestion? Because that seems like it could work

While their suggestion may work, it’s not that optimized and it’s repeated every frame, but you only need to update the health GUI once when the player is damaged or healed. Although you may not have noticed, creating tons of tweens in a short duration can dunk performance.

But it’s completely up to you, I just highly recommend not doing it with runservice.

But this wouldn’t work if a player get’s damaged 2 times in a row right? like while it’s still tweening

That’s why it cancels the tween, so it doesn’t overlap another tween. And the duration it takes from 0-1 makes the delta time for PercentA (which is what the GUI’s scale is at) and PercentB (which is the percent of the health) meaning the time it takes for x percent will always be the same or relative to the total duration possible.

Although issue with the example I provided is since it doesn’t tween healed damage it won’t go to the new health, but you can easily not add that part since it will work without it as well.

So you think that this is still better, cause the loop method might cause lag?

So what would happen if I took damage and immediately heal after it?

Then it would tween to the current health you have.

I can probably make a simple example of this in a moment.

I upped the healing speed significantly so that it will heal during the animation.
https://gyazo.com/a4250809e0ce8266c84394be5e934c84.mp4


Regarding previous reply, it’s not about what I think is better, it’s just between what’s a good habit and what’s not. You’d only resort to loops when you have absolutely no other choice that’s better or more optimized.

This worked! Thankyou alot for the help!

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