Help with tweening Frame base on Health's Value

I can’t figure this out for the love of… I already tried everything and I only know how to tween once and not with this one that continuously change.

local healthIndicator = playerFrame.PlayerImage.HealthIndicator
				local Damage = false
				
				v.HealthChanged:Connect(function()
					local currentHealth = v.Health/v.MaxHealth

					TS:Create(healthIndicator, TweenInfo.new(1), {Transparency = 1 - currentHealth}):Play()
				end)
2 Likes

Hey! Could you provide the full code? You use v.HealthChanged but I can’t tell what v is. Is it a humanoid?

3 Likes

oh thats true, I didn’t realize that

-- Local Script
locPlr.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		for _, v in ipairs(chr:GetDescendants()) do
			if v:IsA("Humanoid") then
				local healthIndicator = playerFrame.PlayerImage.HealthIndicator
				local Damage = false
				
				v.HealthChanged:Connect(function()
					local currentHealth = v.Health/v.MaxHealth

					TS:Create(healthIndicator, TweenInfo.new(1), {Transparency = 1 - currentHealth}):Play()
				end)
			end
		end
	end)
end)
2 Likes

Try using this code. When health changes, it will stop any tween that’s currently playing and immediately start a new one. Having multiple tweens around the same property in one same object should be avoided. Let me know if this fixes it :slight_smile:

local healthIndicator = playerFrame.PlayerImage.HealthIndicator
local Damage = false
local currentTween

v.HealthChanged:Connect(function()
	local currentHealth = v.Health/v.MaxHealth
	if currentTween then
		currentTween:Stop()
	end
	currentTween = TS:Create(healthIndicator, TweenInfo.new(1), {Transparency = 1 - currentHealth})
	currentTween:Play()
end)
1 Like

best way to calculate it is to do MaxHealth/Health

1 Like

tried this and it still won’t show the tween. Just to let everyone know, the Frame is in PlayerGui and its in a LocalScript.

1 Like

Oh okay then. Try this:

-- Local Script
local locPlr = game.Players.LocalPlayer
local chr = locPlr.Character or locPlr.CharacterAdded:Wait()

local function listen_to_humanoid()
	for _, v in ipairs(chr:GetDescendants()) do
		if v:IsA("Humanoid") then
			local healthIndicator = playerFrame.PlayerImage.HealthIndicator
			local Damage = false
				
			v.HealthChanged:Connect(function()
				local currentHealth = v.Health/v.MaxHealth

				TS:Create(healthIndicator, TweenInfo.new(1), {Transparency = 1 - currentHealth}):Play()
			end)
		end
	end
end

locPlr.CharacterAdded:Connect(function(char)
	chr = char
end)
1 Like

maybe you want to reverse it? 1-(MaxHealth/Health)

1 Like

The issue with your code’s that it’s waiting for a new player and their character, and getting that player’s humanoid. It’s not doing it specifically for the player client.
I recommend doing the following:

local PlayersService = game:GetService("Players")

local PlayerClient = PlayersService.LocalPlayer
local HumanoidConn = nil

local function NewCharacter(Character)
	if HumanoidConn ~= nil and HumanoidConn.Connected then -- Is there an HealthChanged connection?
		HumanoidConn:Disconnect() -- Disconnect it
	end
	local Humanoid = Character:WaitForChild("Humanoid")
	local HealthIndicator = playerFrame.PlayerImage.HealthIndicator
	HumanoidConn = Humanoid.HealthChanged:Connect(function(NewHealth) -- HealthChanged passes the player's current health
		local TransparencyAdjustment = CurrentHealth / Humanoid.MaxHealth
		HealthIndicator.Transparency = 1 - TransparencyAdjustment
	end)
end

if PlayerClient.Character ~= nil then -- Did the player's character load?
	NewCharacter(PlayerClient.Character) -- Create a new connection
end
PlayerClient.CharacterAdded:Connect(NewCharacter) -- Create a new connection upon the player's character being created

The advantage of this script is that it’s compatible with StarterPlayerScripts and StarterGui.

oh this really worked!! Thank you so much XD sorry took me a while, I fell asleep last night

however got into a problem and it fades in reverse now

1 Like

I actually found a way and just removed the 1 at HealthIndicator.Transparency, thank youu

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