I have a custom health GUI that I modified from a free model to my own liking, but now I've noticed an issue. I don't want the GUI to reset when the player dies, but the problem with setting ResetPlayerGuiOnSpawn to false makes it so the health bar localscript loses the Humanoid association, which leaves the health bar at 0.
local Player = game.Players.LocalPlayer repeat wait() until Player.Character local Character = Player.Character local Humanoid = Character:WaitForChild("Humanoid") function ChangeSize(Front, DifferenceBar, Algorithm) Front:TweenSize(UDim2.new(Algorithm, 0, 1, 0), "Out", "Linear", 0.5, true) wait(0.5) --btw is this safe to use? DifferenceBar.Size = UDim2.new(Algorithm, 0, 1, 0) end Humanoid.HealthChanged:connect(function(Health) local MaxHealth = Humanoid.MaxHealth local Algorithm = (Health / (MaxHealth)) script.Parent.HPText.Text = math.floor(Humanoid.Health+0.5).. "/"..math.floor(Humanoid.MaxHealth+0.5).. " " ChangeSize(script.Parent.Back.Front, script.Parent.Back.DifferenceBar, Algorithm) end)
How can I grab the player's humanoid when they spawn again without resetting the GUI?
The Player
class provides an excellent method which can solve your problem. What you want to do is use an Event
, more specifically the Player.CharacterAdded
event.
As you can see bellow, when this event is called, it is called with an argument, that of the Player's newly created Character
model.
Player.CharacterAdded:connect(function(Character) end)
All that's left to do now, is fetch the Humanoid
from inside this function and move any code relative to the character inside our newly created event:
Player.CharacterAdded:connect(function(Character) local Humanoid = Character:WaitForChild("Humanoid") Humanoid.HealthChanged:connect(function(Health) --/ Further code here end) end)