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

Why doesn't this code add maxhealth?

Asked by 9 years ago

So the code is suppost to trigger whenever the player respawns the maxhealth goes up to 500, doesn't work on my part, This is made in a localscript.

here's the script:

game.Players.PlayerAdded:connect(function(Char)
    while wait(1) do
        Char.Humanoid.StatusAdded:connect(function()
                Char.Humanoid.MaxHealth = 1000
            Char.Humanoid.Health = Char.Humanoid.MaxHealth
        end)
    end
end)
3
You can't use the 'PlayerAdded' event in a local script. DigitalVeer 1473 — 9y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

There are a few issues here. Veer is correct: PlayerAdded doesn't work with LocalScripts, at least not in a way you would expect, so this has to be done through a normal Script.

PlayerAdded when it fires returns the Player, not their Character, and it only fires once for each Player - when they first join the game. You have to use the CharacterAdded event to catch their respawns.

I'm not sure why you're using the StatusAdded event of Humanoid: nothing internally will fire it, since that associated RBXLibrary was never completed.

This code should work for you: In a Script, preferably in ServerScriptService:

game.Players.PlayerAdded:connect(function(Player)
    Player.CharacterAdded:connect(function(Char)
        wait() --Allow the character to load fully
        Char.Humanoid.MaxHealth = 500 --You said you wanted it set to 500, not 1000
        Char.Humanoid.Health = Char.Humanoid.MaxHealth
    end)
end)
Ad

Answer this question