How to do 2 tween's at once?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want it to cancel the first tween (it will tween because of the natural health regeneration) and prioritize the tween that represents the damage I took
  2. What is the issue? Include screenshots / videos if possible!
    Untitled - Clipped with Medal.tv

It will sometime tween later because of the natural health regen

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’m sure it’s not the tween running late (tested with a print)

Any help would be highly appreciated! And let me know if you have any questions.

16 Likes

Its hard to tell from the video only, do you have a code snippet you can share?

6 Likes

Here, this get’s executed everytime my health changes.

script.Parent:TweenSize(UDim2.new(Player.Character.Humanoid.Health / Player.Character.Humanoid.MaxHealth,0,1,0))

The issue is that the health can change 2 times very fast after eachother if u naturally heal and take damage

3 Likes

create a bool value
if bool = false then
-set bool = true
-tween
-set bool = false
else wait until bool = false
-set bool = true
-tween
-set bool = false

this way, it only runs the tween function after the previous one is completed.

4 Likes

Yes but then it’ll completely ignore the (most important) damage tween if u heal right before getting damaged

3 Likes

Here’s some code I use in my game for a Stamina bar which works the exact same way as your Health Bar (constantly regenerates and immediately cuts down when a player does smth) changed a little bit to fit your case

local TS = game:GetService("TweenService")

local HealthBar = script.Parent
while task.wait() do
	local Health = player.Character.Humanoid.Health
    local MaxHealth = player.Character.Humanoid.MaxHealth
	TS:Create(HealthBar , TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Size = UDim2.fromScale(Health / MaxHealth, 1)}):Play()	
end

Try it and see if it works

Edit: I suggest you either put this loop in a coroutine or call it with a Remote or Bindable Event so it doesn’t halt the rest of your script

2 Likes

that’s very inefficient
just use a variable for this situation

3 Likes

its best to use runService instead of while true do because these infinite loops can cause memory leaks if you are not careful.

here’s something that can replace a loop:

runService.Heartbeat:Connect(function()

end)
4 Likes

Won’t a loop lag the game? Like isn’t looping task.wait() very, very laggy?

3 Likes

How fast will this run? Like is it faster than task.wait() or?

3 Likes

task.wait and loops do not necessarily lag the game. it’s an overuse of them and improper coding inside of them that can lead to complications

3 Likes

Alright thankyou. Do I need to add a task.wait() inside the heartbeat thing or no? I’ve never used it before. Is it a straight upgrade from a while true loop? Cause in that case i’ll replace all my loops with it

3 Likes

runservice has many different events that you can connect to functions. most of these events are way more optimal than task.wait()

TLDR: Basically, wait() is unoptimized and at default is between .1 and .3 seconds (never the same), whilst .Heartbeat, .Stepped, .RenderStepped actually runs the function 60 times per second (Technically as fast as you can get it). Read info about renderstepped because you don’t want to use it all the time.

.Heartbeat is fired once after a frame has been rendered. This basically means that once every 60 seconds, Roblox’s graphics engine will render the game and then after that, call the heartbeat function.

.RenderStepped is fired prior to every frame render. This means that you are putting anything that processing at a higher priority than the frame being loaded. Only use renderstepped if you want to change something about the camera because changes will be made before the frame is rendered.

task.wait() on the other hand uses a system that was originally made for pausing the thread until an event is processed. When using task.wait() or wait() on its own, the default wait time is about 0.3 seconds.

4 Likes

I agree with chiseled_cheese runService.Heartbeat should work better than
while task.wait() do does
It fires every frame so it doesn’t need a task.wait() in it.

2 Likes

yeah, wait() is not used correctly. i think it should only be used in situations like this:

tween.Finished:Wait()

never use it to precisely wait for time to pass by

Also this might be a bit obvious but remember to initialize the runService variable before using heartbeat

local runService = game:GetService("RunService")
1 Like

im sorry, i didn’t read the problem fully yet

i think the solution to this is just to make the tween shorter, or not even use a tween at all. instead, give the screen a little shake effect or like a pulse that emits from the healthbar whenever the health is lowered. then, only tween it if the health increases.

the reason behind this is because you want the player to know that they are losing health, so an instant or fast jump in the healthbar size accompanied by a small shake does the job.

the fact that a player is healing should not grab their attention as much, so therefore tween it smoothly. that also makes sense for healing since it should be smooth and calm.

Yea this could be a feature but its not necessary. Since the health they are losing is the default Humanoid.Health the game flashes a red effect around the screen everytime you take damage (as shown in the video too) which is enough to alert the player, maybe a notification when you’re low on health? idk, either way it seems like a good addition to make the game different/unique.

i think your current health bar lacks some good effects though because it just constantly smoothly tweens no matter what happens to the characters health. roblox gives you a start with that red flash effect. that doesn’t mean that’s all you need

I used this health bar since OP’s problem was to make the damage-taking tween cancel out a health-regenerating tween. Most players will realise they are taking damage from the red flash but if OP want to improve their game then yeah they can add some extra effects im not against it.