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

Trying to access the health of the player, isn't working?

Asked by
Uzuntu 6
6 years ago

I'm making a script that assigns a NumberValue to the player that is equivalent to their health, however, it either says that the character is a nil value, or the humanoid/character is not a valid member of player. Any help?

function onPlayerAdded(player)

    local ValueFolder = Instance.new("Folder")
    ValueFolder.Parent = player
    ValueFolder.Name = "ValueFolder"


    local PlayerHealth = Instance.new("NumberValue")
    PlayerHealth.Parent = player.ValueFolder
    PlayerHealth.Name = "PlayerHealth"
    PlayerHealth.Value = player.Character.Humanoid.Health   

end

game:GetService("Players").PlayerAdded:connect(onPlayerAdded)

2 answers

Log in to vote
1
Answered by 6 years ago

Try waiting for the character to be added to the workspace, not when the player is added to the game.

.CharacterAdded:Connect(character)

and then use character:WaitForChild("Humanoid") to wait for the humanoid

If this doesn't make sense, just ask.

Ad
Log in to vote
0
Answered by 6 years ago

Try this:

function onPlayerAdded(player)

    local ValueFolder = Instance.new("Folder")
    ValueFolder.Parent = player
    ValueFolder.Name = "ValueFolder"

    player.CharacterAdded:Connect(function(character)
        local PlayerHealth = Instance.new("NumberValue")
        PlayerHealth.Parent = player.ValueFolder
        PlayerHealth.Name = "PlayerHealth"
        PlayerHealth.Value = character:WaitForChild("Humanoid").Health
    end)

end

game:GetService("Players").PlayerAdded:connect(onPlayerAdded)

Hope this helped!

Answer this question