I’m trying to detect a change in the value of Humanoid.Health, but for some reason it only works when the character dies (not after it’s died, only when it dies). I’ve tried Humanoid:GetPropertyChangedSignal("Health"), Humanoid.HealthChanged and Humanoid.Changed
Heres my code (this is running on the server):
local function setupUI(plr: Player)
local plrUI = ui.CrewMember:Clone()
plrUI.Parent = frame
plrUI.Name = plr.Name
plrUI:WaitForChild("CrewName").Text = plr.Name
local healthConnection
local charConnection
local char = plr.Character or plr.CharacterAdded:Wait()
local hum: Humanoid = char:WaitForChild("Humanoid")
local function onHealthChanged()
print("hc")
plrUI.Health.Bar.Size = UDim2.fromScale(hum.Health / 100,1)
end
healthConnection = hum:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
charConnection = plr.CharacterAdded:Connect(function(char)
print("char added")
healthConnection:Disconnect()
hum = char:WaitForChild("Humanoid")
plrUI.Health.Bar.Size = UDim2.fromScale(1,1)
healthConnection = hum:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
game.Players.PlayerRemoving:Connect(function(player)
if player == plr then
plrUI:Destroy()
diedConnection:Disconnect()
end
end)
end)
end
You do not need to set the text of a player’s GUI from the server! You don’t have to bother manually removing the GUI either! Put this as a LocalScript under ui.CrewMember:
local plrUI = script.Parent -- put this script under `ui.CrewMember`, or make a copy and parent locally
plrUI:WaitForChild("CrewName").Text = game.Players.LocalPlayer.Name
local charAdded = function(character)
local humanoid = character:WaitForChild("Humanoid")
local function onHealthChanged()
plrUI.Health.Bar.Size = UDim2.fromScale(humanoid.Health / 100, 1)
end
humanoid:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
end
game.Players.LocalPlayer.CharacterAdded:Connect(charAdded)
if game.Players.LocalPlayer.Character then charAdded(game.Players.LocalPlayer.Character) end
Try this (Server script)
Presumably setupUI should be called in PlayerAdded
local ui = {}
local function setupUI(plr: Player)
local plrUI = ui.CrewMember:Clone()
plrUI.Parent = frame
plrUI.Name = plr.Name
plrUI:WaitForChild("CrewName").Text = plr.Name
if ui[plr] then ui[plr]:Destroy() end
ui[plr] = plrUI
local charAdded = function(character)
local humanoid = character:WaitForChild("Humanoid")
local function onHealthChanged()
plrUI.Health.Bar.Size = UDim2.fromScale(humanoid.Health / 100, 1)
end
humanoid:GetPropertyChangedSignal("Health"):Connect(onHealthChanged)
end
plr.CharacterAdded:Connect(charAdded)
if plr.Character then charAdded(plr.Character) end
end
game.Players.PlayerRemoving:Connect(function(plr)
if ui[plr] then ui[plr]:Destroy() end
ui[plr] = nil
end
It’s better if you clean up your code, since that PlayerRemoving connection can most likely cause a memory leak!
Also, the code seems to be working for me! Are you changing the health locally or on the server? The Health property of the Humanoid doesn’t replicate to the server, but only does when the player dies!
Thank you so much dude! You are incredible !
Im going to go test it now! Thanks again man !
This is gonna be really helpful!
okay bye now !
see you later!
!!!