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

Can't figure out how to get Humanoid Health from an event?

Whenever I try to use this, it attempts to index "character" but it is a nil value, what would I use to solve this?

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) 

	print(player.Character.Humanoid.Health) 

9 comments

0

By the way you should be able to accept my answer which should be under the answer itself, or even hovering over the answer might prompt you to accept.

0

I wonder if because I have -5 reputation I can't accept it, but I am not seeing it for some reason, but I will check thank you

0

Try it now.

0

It must be my browser or something, I'm not sure :( https://imgur.com/a/0M2WuQT is a picture of the screen, I still am not seeing it for some reason

0

Try hovering over the answer see if a little prompt that tells you to accept it. Otherwise I do not know

0

My account is pretty new, could that be it? and is there a certain part of the answer or just the box that the answer is contained in?

0

As far as im aware there are no restrictions to being able to accept answers.

0

I am so sorry I will try to figure this out :/ I might need to get help from support for this.

2 answers

✓ Accepted answer

Problem

The character is not loaded instantly when a player joins. Your code is executing too soon.

Solution

Execute this code in a CharacterAdded event listener.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) 
    player.CharacterAdded:Connect(function(character)
        print(character:WaitForChild("Humanoid").Health)
    end)
end)

However it's also possible that the reverse can also occur. Your event listener could have been established too late so you'd have to wait for the character to respawn. Just before establishing your event listener you should check if the character first exists.

Players.PlayerAdded:Connect(function(player) 
    if player.Character then
        print(player.Character:WaitForChild("Humanoid").Health)
    end

    player.CharacterAdded:Connect(function(character)
        print(character:WaitForChild("Humanoid").Health)
    end)
end)

1 comment

0

Thank you so much

You're likely trying to print it before the character exists. If we use CharacterAdded we can be sure the character exists before printing.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player) 

	player.CharacterAdded:Connect(function(character)

		print(character.Humanoid.Health) 

	end)

end)

5 comments

0

how would I accept the answer? Thank you so much

0

It says it on the right side of the answer

0

oof low rep people can't accept answers

0

Are you sure about that?

0

I was able to finally accept it thank you!! =)

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