Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I regrab the player's humanoid from a PlayerGUI when ResetPlayerGuiOnSpawn is false?

Asked by 7 years ago

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?

1 answer

Log in to vote
0
Answered by 7 years ago

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)
1
Hey, that works! The only problem I have with this however is that the health bar only works after the player dies once. Before, nothing inside the function runs. sonickyle 2 — 7y
1
Wait never mind it works now after I moved some code into the function. Thanks! sonickyle 2 — 7y
Ad

Answer this question