Issues with getting multiple tweens to play after another on low health

1.My main goal is to get a low Health effect that I have made to half reverse itself once the tweens finish.
2. The main issue that I am facing is that once the tweens play don’t reverse
(Please note that I am not referring to “bool reverses” I made my own reverse tween and that is what I’m having issues with.)
3. I’ve tried adding a simple wait(1.3) before the reversing tweens play.
I’ve also tried just using the tween delay feature “number DelayTime”.
and the last thing ive tried using is

tween4.Completed:Connect(function(playbackState)
	tween5:Play()
end)

Here it is in action:

(I am also having issues with keeping the heart in the center when resizing it.)

here is the code in question:

tween1.Completed:Connect(function(playbackState)
	tween8:Play()
end)

tween2.Completed:Connect(function(playbackState)
	tween7:Play()
end)

tween3.Completed:Connect(function(playbackState)
	tween6:Play()
end)

tween4.Completed:Connect(function(playbackState)
	tween5:Play()
end)


while true do
	if script.Parent.Humanoid.Health <= 40 then
		game.Lighting.ColorCorrection.Enabled = true
		game.Lighting.Blur.Enabled = true
		player3.heart.Enabled = true
		tween1:Play()
		tween2:Play()
		tween3:Play()
		tween4:Play()
	else
		game.Lighting.ColorCorrection.Enabled = false
		game.Lighting.Blur.Enabled = false
		player3.heart.Enabled = false
	end
	wait()
end

the rest is just tweens and variables :slightly_smiling_face:

1 Like

It’s actually pretty simple. In your code, you used a forever loop with a wait() in between each loop. So let’s simulate the situation:
Frame 1: Health is full, so turn off the visual effects.
Frame 2: Health is still full, turn off the visual effects again.
Frame 3: Now the health is 30, so turn on the visual effects and start the tween.
Frame 4: The health is still at 30, turn on the visual effects and start another tween.
Frame 5: The health is still at 30, turn on the visual effects and start another tween.

So you get the idea. Because you check the health every single loop, with only a wait() in between, the tween is constantly starting over and over, so the server is probably really confused. I don’t know what behavior will be produced, would the new tween override the old tween? Or would they balance each other out? I don’t know. I would suggest connecting this instead to an event, which would not only solve this problem, but also make it easier on the server. Like this:

script.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if script.Parent.Humanoid.Health <= 40 then
		game.Lighting.ColorCorrection.Enabled = true
		game.Lighting.Blur.Enabled = true
		player3.heart.Enabled = true
		tween1:Play()
		tween2:Play()
		tween3:Play()
		tween4:Play()
	else
		game.Lighting.ColorCorrection.Enabled = false
		game.Lighting.Blur.Enabled = false
		player3.heart.Enabled = false
	end
end

Or if you want the tween to occur repeatedly while the player is at low health:

script.Parent.Humanoid:GetPropertyChangedSignal("Health"):Connect(function() -- Only calls the function when the player's health value changes
	if script.Parent.Humanoid.Health <= 40 then
		game.Lighting.ColorCorrection.Enabled = true
		game.Lighting.Blur.Enabled = true
		player3.heart.Enabled = true
		while script.Parent.Humanoid.Health <= 40 do -- Visual effect repeats as long as the player is in low health
			tween1:Play()
			tween2:Play()
			tween3:Play()
			tween4:Play()
			tween1.Completed:Wait() -- You could change this to something like wait(1) or some other number depending on how often you want the effect to loop
			tween8.Completed:Wait() -- You could change this to something like wait(1) or some other number depending on how often you want the effect to loop
		end
	else
		game.Lighting.ColorCorrection.Enabled = false
		game.Lighting.Blur.Enabled = false
		player3.heart.Enabled = false
	end
end

Also, about your problem with keeping the heart in the center. There’s only one fact you need to know. Roblox’s GUI objects’ positions are based on their top left corners, which means that if you want to do something like centering the heart, you probably need to do some calculation like:

local midX = 0.5
local midY = 0.5
heart.Position = UDim2.new(midX - heart.Size.X.Scale / 2, 0, midY - heart.Size.Y.Scale / 2, 0)

But you could change this to Offset instead of Scale depending on how your GUI is formatted. Basically just subtract half the GUI’s size from the center position.

I hope this helps solve your problem!

3 Likes
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(health)
	if health <= 40 then
		--do code
	elseif health > 40 then
		--do other code
	end
end)

You can also use the HealthChanged event which is more optimal as it doesn’t create an event listener in the script which waits for the value of any property of the Humanoid instance to change, instead only the Health property of the Humanoid instance is listened to.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

1 Like