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 âŚ
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
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.