Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.