-- 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)
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
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)
-- 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)
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.