New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by
Uzuntu 6
2 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 2 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 2 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